File size: 673 Bytes
b5ea024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"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; });
		}
	);
};