Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- // the wrapper
- function addPromiseToCallback(fn) {
- const result = function () {
- fn(...arguments);
- result.resolve([...arguments]);
- };
- result.promise = new Promise((resolve) => {
- result.resolve = resolve;
- });
- return result;
- }
- // an example third party function that calls a callback
- // in my case this would be the AWS Lambda infrastructure
- function callsCallback(cb) {
- setTimeout(() => cb('giraffe', Date.now()), 100);
- }
- // my callback function
- function myCallback(arg1, arg2) {
- console.log(`called back! ${arg1}, ${arg2}`);
- }
- const callbackWithPromise = addPromiseToCallback(myCallback);
- callsCallback(callbackWithPromise);
- callbackWithPromise.promise
- .then((args) => {
- console.log(`Promise 1 resolved with ${args[0]}, ${args[1]}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement