al6x / synchronize

Write asynchronous code as if it's synchronous
http://alexeypetrushin.github.com/synchronize
316 stars 57 forks source link

allow return result and error instead of throw error #59

Open itswisdomagain opened 6 years ago

itswisdomagain commented 6 years ago

Different calls may produce different errors that may need to be handled differently. Users can set a flag to disable throwing exceptions on error.

Examples

var deferReturnError = sync.defer(true);
sync.fiber(function() {
    var fnCallResult = sync.await(asyncFunction(deferReturnError));
    var err = fnCallResult.error; var result = fnCallResult.result;
}
synchronizedFn = sync(asyncFunction);
sync.fiber(function() {
    var fnCallResult = synchronizedFn(arg1, arg2, sync.returnErrorValue);
    var err = fnCallResult.error; var result = fnCallResult.result;
}

Or ES6 style

var deferReturnError = sync.defer(true);
sync.fiber(function() {
    const { error, result } = sync.await(asyncFunction(deferReturnError));
}
synchronizedFn = sync(asyncFunction);
sync.fiber(function() {
    const { error, result }  = synchronizedFn(arg1, arg2, sync.returnErrorValue);
}