Created
April 13, 2020 10:58
-
-
Save Enelar/adc620f262a0cdb00f45f7b50eb0b09d to your computer and use it in GitHub Desktop.
Quick to promise method
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
to_direct_promise(functor, ...args) { | |
return this.to_promise(functor, ...args, undefined); | |
} | |
to_promise(functor, ...args) { | |
const options = args.pop(); | |
let resolve; | |
let reject; | |
const result = new Promise((res, cat) => { | |
resolve = res; | |
reject = cat; | |
}); | |
functor.call(this, ...args, (error, data) => { | |
if (error) { | |
return reject(error); | |
} | |
return resolve(data); | |
}, options); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment