UNPKG

442 BJavaScriptView Raw
1import Promise from 'es6-promise/lib/es6-promise/promise.js';
2
3// Return the first non-falsy result from an array of
4// maybe-sync, maybe-promise-returning functions
5export default function first ( candidates ) {
6 return function ( ...args ) {
7 return candidates.reduce( ( promise, candidate ) => {
8 return promise.then( result => result != null ?
9 result :
10 Promise.resolve( candidate( ...args ) ) );
11 }, Promise.resolve() );
12 }
13}