mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-25 09:55:15 +00:00
25 lines
706 B
JavaScript
25 lines
706 B
JavaScript
|
"use strict";
|
||
|
|
||
|
var ensurePlainFunction = require("../../../object/ensure-plain-function")
|
||
|
, isThenable = require("../../../object/is-thenable")
|
||
|
, ensureThenable = require("../../../object/ensure-thenable");
|
||
|
|
||
|
var resolveCallback = function (callback, next) {
|
||
|
var callbackResult = callback();
|
||
|
if (!isThenable(callbackResult)) return next();
|
||
|
return callbackResult.then(next);
|
||
|
};
|
||
|
|
||
|
module.exports = function (callback) {
|
||
|
ensureThenable(this);
|
||
|
ensurePlainFunction(callback);
|
||
|
return this.then(
|
||
|
function (result) {
|
||
|
return resolveCallback(callback, function () { return result; });
|
||
|
},
|
||
|
function (error) {
|
||
|
return resolveCallback(callback, function () { throw error; });
|
||
|
}
|
||
|
);
|
||
|
};
|