mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
9 years ago
3 changed files with 15 additions and 19 deletions
@ -1,11 +0,0 @@ |
|||||
// Given a `fallback` and a `preferred` function,
|
|
||||
// return a function that attempts to call the `preferred`.
|
|
||||
// If it fails, or returns undefined, call the fallback and return its value.
|
|
||||
export default function attempt ( fallback, preferred ) { |
|
||||
return function ( ...args ) { |
|
||||
const boundFallback = () => fallback( ...args ); |
|
||||
|
|
||||
return Promise.resolve( preferred( ...args ) ) |
|
||||
.then( res => res === undefined ? boundFallback() : res, boundFallback ); |
|
||||
}; |
|
||||
} |
|
@ -0,0 +1,11 @@ |
|||||
|
// Return the first non-falsy result from an array of
|
||||
|
// maybe-sync, maybe-promise-returning functions
|
||||
|
export default function first ( candidates ) { |
||||
|
return function ( ...args ) { |
||||
|
return candidates.reduce( ( promise, candidate ) => { |
||||
|
return promise.then( result => result != null ? |
||||
|
result : |
||||
|
Promise.resolve( candidate( ...args ) ) ); |
||||
|
}, Promise.resolve() ); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue