-
Notifications
You must be signed in to change notification settings - Fork 32
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
02548b5
commit 647196e
Showing
6 changed files
with
246 additions
and
143 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,67 +1 @@ | ||
// Symbols is a better way to do this, but not all browsers have good support, | ||
// so instead we'll just make do with a very unlikely string. | ||
var customArgumentsToken = "__ES6-PROMISIFY--CUSTOM-ARGUMENTS__"; | ||
/** | ||
* promisify() | ||
* Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- | ||
* into an ES6-compatible Promise. Promisify provides a default callback of the | ||
* form (error, result) and rejects when `error` is truthy. | ||
* | ||
* @param {function} original - The function to promisify | ||
* @return {function} A promisified version of `original` | ||
*/ | ||
|
||
export function promisify(original) { | ||
// Ensure the argument is a function | ||
if (typeof original !== "function") { | ||
throw new TypeError("Argument to promisify must be a function"); | ||
} // If the user has asked us to decode argument names for them, honour that | ||
|
||
|
||
var argumentNames = original[customArgumentsToken]; // If the user has supplied a custom Promise implementation, use it. | ||
// Otherwise fall back to whatever we can find on the global object. | ||
|
||
var ES6Promise = promisify.Promise || Promise; // If we can find no Promise implemention, then fail now. | ||
|
||
if (typeof ES6Promise !== "function") { | ||
throw new Error("No Promise implementation found; do you need a polyfill?"); | ||
} | ||
|
||
return function () { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
|
||
return new ES6Promise((resolve, reject) => { | ||
// Append the callback bound to the context | ||
args.push(function callback(err) { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
values[_key2 - 1] = arguments[_key2]; | ||
} | ||
|
||
if (values.length === 1 || !argumentNames) { | ||
return resolve(values[0]); | ||
} | ||
|
||
var o = {}; | ||
values.forEach((value, index) => { | ||
var name = argumentNames[index]; | ||
|
||
if (name) { | ||
o[name] = value; | ||
} | ||
}); | ||
resolve(o); | ||
}); // Call the function. | ||
|
||
original.apply(this, args); | ||
}); | ||
}; | ||
} // Attach this symbol to the exported function, so users can use it | ||
|
||
promisify.argumentNames = customArgumentsToken; | ||
promisify.Promise = undefined; | ||
var customArgumentsToken="__ES6-PROMISIFY--CUSTOM-ARGUMENTS__";export function promisify(a){if("function"!=typeof a)throw new TypeError("Argument to promisify must be a function");var b=a[customArgumentsToken],c=promisify.Promise||Promise;if("function"!=typeof c)throw new Error("No Promise implementation found; do you need a polyfill?");return function(){for(var d=arguments.length,e=Array(d),f=0;f<d;f++)e[f]=arguments[f];return new c((c,d)=>{e.push(function(a){if(a)return d(a);for(var e=arguments.length,f=Array(1<e?e-1:0),g=1;g<e;g++)f[g-1]=arguments[g];if(1===f.length||!b)return c(f[0]);var h={};f.forEach((a,c)=>{var d=b[c];d&&(h[d]=a)}),c(h)}),a.apply(this,e)})}}promisify.argumentNames="__ES6-PROMISIFY--CUSTOM-ARGUMENTS__",promisify.Promise=void 0; |
Oops, something went wrong.