Skip to content

Commit 8ca1481

Browse files
committed
Merge branch 'feature/promises-a+' into develop
2 parents 7ff2223 + af23ed0 commit 8ca1481

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,17 @@ async function foo(req,res) {
8181
}
8282
```
8383

84+
## Promises/A+ compliant support
85+
You never know what kind of promise you'll get from an external dependency. It can be from Bluebird, Babel polyfill or some other library. `on` should work with any promise object can `then`, such as the one provided by the popular [`bluebird`](https://github.com/petkaantonov/bluebird/) package:
86+
87+
```
88+
const {on} = require('await-on');
89+
const Bluebird = require('bluebird')
90+
91+
const fetchData = () => new Bluebird(/*...*/);
92+
const [err, data] = await on(fetchData());
93+
```
94+
95+
8496
## License
8597
MIT License. See [License](https://github.com/bitstrider/await-on/blob/master/LICENSE) in the repository.

lib/await-on.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @return {Array} Array with signature `[err, data]`
77
*/
88
function on(promise) {
9-
if(promise instanceof Promise) {
9+
if(promise && typeof promise.then === 'function') {
1010
return promise.then(data => {
1111
return [null, data];
1212
})

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"author": "Jason Yung",
2929
"license": "MIT",
3030
"devDependencies": {
31+
"bluebird": "^3.5.4",
3132
"tap": "^10.7.2"
3233
},
3334
"engines": {

test/core.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const tap = require('tap')
2+
const Bluebird = require('bluebird')
23
const on = require('../lib/await-on')
34
const ANSWER = 42
45
const ERROR = new Error('Need more time to figure that out')
56

6-
let waitOne = (answer=ANSWER) => new Promise(resolve => setTimeout(() => resolve(answer),1000))
7+
let waitOne = (answer=ANSWER, PromiseConstructor = Promise) => new PromiseConstructor(resolve => setTimeout(() => resolve(answer),10))
78
let waitOneAndThrow = () => waitOne().then(res => Promise.reject(ERROR))
89

910
tap.test("on", async function (t) {
@@ -15,6 +16,15 @@ tap.test("on", async function (t) {
1516
t.equal(res,ANSWER)
1617
})
1718

19+
tap.test("on thenable", async function (t) {
20+
t.plan(2)
21+
22+
const [err,res] = await on(waitOne(ANSWER, Bluebird))
23+
24+
t.notOk(err)
25+
t.equal(res,ANSWER)
26+
})
27+
1828
tap.test("@handler", async function (t) {
1929
t.plan(2)
2030

0 commit comments

Comments
 (0)