hybridables / redolent Goto Github PK
View Code? Open in Web Editor NEWSimple promisify with sane defaults, works on node 0.10 if you provide custom Promise through options
Home Page: http://j.mp/1LLnFgE
License: MIT License
Simple promisify with sane defaults, works on node 0.10 if you provide custom Promise through options
Home Page: http://j.mp/1LLnFgE
License: MIT License
e.g.
var fn = redolent(function (done) {
return 'foo bar'
})
fn().catch(function (err) {
console.log(err)
})
It can only return a Promise or undefined, or call the callback.
in these cases it not rejects
var fn = redolent(function (done) {
return
})
var fn = redolent(function (done) {
return undefined
})
Because it is one of the main mechanisms behind the new wave of hybrids and upcoming stable v1 releases cycle.
should be true
by default, e.g. multipleArgs !== false
on L59 if statement
Almost the same as #18
if (isAsyncFn && !called && !isPromise(syncResult)) {
var msg = 'async function need to call `done/cb` callback argument or return a Promise'
reject(new Error('redolent: ' + msg))
}
for example
var fn = redolent(function (foo, done) {
console.log('foo is', foo)
// done is not called nor Promise is returned,
// so when `fn` is called it will return rejected promise
})
fn(123).catch(console.log)
Currently it will return rejected promise too, but with no so clear message - boolean is not a function
which means nothing to the user.
When for example, there is done
argument, but is not called, and there is a result which is a promise.
something like
if (isAsyncFn && !called && isPromise(syncResult)) {
resolve(syncResult)
}
https://github.com/hybridables/redolent/blob/master/index.js#L117-L124
edit:
this seems to fix it
if ((!isAsyncFn && !called) || (isAsyncFn && !called && isPromise(syncResult))) {
resolve(syncResult);
}
i think we should handle this case, in that way. It is a lot better to auto-magically do it, instead of returning rejected promise that signals the user that he didn't called the done
callback argument.
for example
var fn = redolent(function * () {return yield args})
fn(1, 2, 3).then(console.log) // => [1, 2, 3]
for more lazyness, add browserify-friendly utils.js
file
so we can just
var PromiseCtor = redolent.load()
// or
var PromiseCtor = redolent.load(require('bluebird')) // or some other promise
working code from the bundle (index$10 is NativePromise, index$6 extend-shallow)
function loadPromise (Promize) {
var promCtor = index$10 || Promize
if (typeof promCtor !== 'function') {
throw new TypeError('redolent: no native Promise support and no opts.Promise')
}
return promCtor
}
var index = function redolent (fn, opts) {
if (typeof fn !== 'function') {
throw new TypeError('redolent: expect `fn` to be a function')
}
opts = index$6({
context: this,
Promise: index$10
}, opts);
opts.Promise = loadPromise(opts.Promise)
module.exports.Promise = opts.Promise
redolent.Promise = opts.Promise
index.Promise = opts.Promise
return function () {
opts.context = this || opts.context;
opts.args = index$2(opts.args).concat(index$4(arguments));
// if (typeof opts.Promise !== 'function') {
// throw new TypeError('redolent: no native Promise support and no opts.Promise')
// }
var promise = new opts.Promise(function (resolve, reject) {
var called = false;
function done (er, res) {
called = true;
if (er) {
return reject(er)
}
if (arguments.length > 2) {
res = index$4(arguments, 1);
}
return resolve(res)
}
var isAsyncFn = index$12(fn);
opts.args = isAsyncFn ? opts.args.concat(done) : opts.args;
var syncResult = fn.apply(opts.context, opts.args);
if (!isAsyncFn && !called) {
resolve(syncResult);
}
});
return normalize(promise, index$10)
}
};
function normalize (promise, isNativeSupported) {
promise.___nativePromise = Boolean(isNativeSupported);
promise.___customPromise = !promise.___nativePromise;
return promise
}
index.load = loadPromise
module.exports = index;
co@4
testsunhandledRejection
and uncaughtException
events (* remove them from [email protected]
).promisify
methodhybridify@2
hybridables/hybridify#4callback2stream
hybridables/callback2stream#7because always pass "callback" function and when sync function is given optional arguments it throws
TypeError: Expected options to be either an object or a string, but got function instead
rised hybridables/letta#2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.