You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.6 KiB
52 lines
1.6 KiB
var toArr = require('./toArr'); |
|
var isGeneratorFn = require('./isGeneratorFn'); |
|
var isPromise = require('./isPromise'); |
|
var toStr = require('./toStr'); |
|
|
|
exports = function(fn) { |
|
if (!isGeneratorFn(fn)) { |
|
throw new TypeError('Expected a generator function'); |
|
} |
|
return function() { |
|
var _this = this; |
|
var args = toArr(arguments); |
|
return new Promise(function(resolve, reject) { |
|
var generator = fn.apply(_this, args); |
|
function onFulfilled(res) { |
|
var ret; |
|
try { |
|
ret = generator.next(res); |
|
} catch (e) { |
|
return reject(e); |
|
} |
|
next(ret); |
|
} |
|
function onRejected(err) { |
|
var ret; |
|
try { |
|
ret = generator.throw(err); |
|
} catch (e) { |
|
return reject(e); |
|
} |
|
next(ret); |
|
} |
|
function next(ret) { |
|
if (ret.done) return resolve(ret.value); |
|
if (isPromise(ret.value)) { |
|
return ret.value.then(onFulfilled, onRejected); |
|
} |
|
return onRejected( |
|
new TypeError( |
|
'You may only yield a promise, '.concat( |
|
toStr(ret.value), |
|
' is passed' |
|
) |
|
) |
|
); |
|
} |
|
onFulfilled(); |
|
}); |
|
}; |
|
}; |
|
|
|
module.exports = exports;
|
|
|