awaitable callback.
async/await way to call callback style functions
deprecated
Ownership of this package has been transferred, please use [@superjs/awaitor](https://www.npmjs
.com/package/@superjs/awaitor)
thought
If you want to convert callback style to async/await style, you can either:
- wrap the operation to return a promise
- just call the operation, using @mybug/awaitor as callback, like the example below
usage
awaitor()
return an awaitable callback, the promiseValue of which is the args array it's called with
awaitor.norm()
same as above, but it only accept two args: (err,value)
. If err exists, promiseValue will be err
and rejected.
Otherwise, promiseValue will be value
.
example
const awaitor = require('@mybug/awaitor')
;(async ()=>{
let cb = awaitor()
foo(1,2,cb)
let result = await cb
console.log(result)
})()
function foo(a,b,cb){
setTimeout(()=>cb(a+b),1000)
}
wait can await something synchronously.
const awaitor = require('@mybug/awaitor')
const wait = require('@mybug/wait')
let cb = awaitor()
foo(1,2,cb)
let result = wait(cb)
console.log(result)
function foo(a,b,cb){
setTimeout(()=>cb(a+b),1000)
}
compatibility
You can use @mybug/awaitor in both nodejs and browser environment.