This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
020c2cd
commit e9250d1
Showing
6 changed files
with
43 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
language: node_js | ||
node_js: | ||
- '12' | ||
- '10' | ||
- '8' | ||
- '6' | ||
- '4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
'use strict'; | ||
module.exports = (promise, onFinally) => { | ||
onFinally = onFinally || (() => {}); | ||
|
||
return promise.then( | ||
val => Promise.resolve(onFinally()).then(() => val), | ||
err => Promise.resolve(onFinally()).then(() => { | ||
throw err; | ||
}) | ||
); | ||
module.exports = async ( | ||
promise, | ||
onFinally = (() => {}) | ||
) => { | ||
try { | ||
const value = await promise; | ||
await onFinally(); | ||
return value; | ||
} catch (error) { | ||
await onFinally(); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
import test from 'ava'; | ||
import m from '.'; | ||
import pFinally from '.'; | ||
|
||
const fixture = Symbol('fixture'); | ||
const fixtureErr = new Error('err'); | ||
const fixtureError = new Error('error'); | ||
|
||
test('does nothing when nothing is passed', async t => { | ||
t.is(await m(Promise.resolve(fixture)), fixture); | ||
t.is(await pFinally(Promise.resolve(fixture)), fixture); | ||
}); | ||
|
||
test('callback is called when promise is fulfilled', async t => { | ||
let called = false; | ||
let isCalled = false; | ||
|
||
const val = await m(Promise.resolve(fixture), () => { | ||
called = true; | ||
const value = await pFinally(Promise.resolve(fixture), () => { | ||
isCalled = true; | ||
}); | ||
|
||
t.is(val, fixture); | ||
t.true(called); | ||
t.is(value, fixture); | ||
t.true(isCalled); | ||
}); | ||
|
||
test('callback is called when promise is rejected', async t => { | ||
let called = false; | ||
let isCalled = false; | ||
|
||
await m(Promise.reject(fixtureErr), () => { | ||
called = true; | ||
}).catch(err => { | ||
t.is(err, fixtureErr); | ||
await pFinally(Promise.reject(fixtureError), () => { | ||
isCalled = true; | ||
}).catch(error => { | ||
t.is(error, fixtureError); | ||
}); | ||
|
||
t.true(called); | ||
t.true(isCalled); | ||
}); | ||
|
||
test('returning a rejected promise in the callback rejects the promise', async t => { | ||
await m(Promise.resolve(fixture), () => Promise.reject(fixtureErr)).then(() => { | ||
await pFinally(Promise.resolve(fixture), () => Promise.reject(fixtureError)).then(() => { | ||
t.fail(); | ||
}, err => { | ||
t.is(err, fixtureErr); | ||
}, error => { | ||
t.is(error, fixtureError); | ||
}); | ||
}); | ||
|
||
test('returning a rejected promise in the callback for an already rejected promise changes the rejection reason', async t => { | ||
await m(Promise.reject(new Error('orig err')), () => Promise.reject(fixtureErr)).catch(err => { | ||
t.is(err, fixtureErr); | ||
await pFinally(Promise.reject(new Error('original error')), () => Promise.reject(fixtureError)).catch(error => { | ||
t.is(error, fixtureError); | ||
}); | ||
}); |