mirror of
https://github.com/JasonYANG170/IOTConnect-Web.git
synced 2024-11-23 20:26:28 +00:00
25 lines
673 B
JavaScript
25 lines
673 B
JavaScript
|
"use strict";
|
||
|
|
||
|
var ensurePlainFunction = require("type/plain-function/ensure")
|
||
|
, isThenable = require("type/thenable/is")
|
||
|
, ensureThenable = require("type/thenable/ensure");
|
||
|
|
||
|
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; });
|
||
|
}
|
||
|
);
|
||
|
};
|