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.
28 lines
481 B
28 lines
481 B
const { isPromise } = require('../../util');
|
|
|
|
const caller = (test, state) => {
|
|
|
|
let value, error;
|
|
try {
|
|
value = test(state);
|
|
} catch (e) {
|
|
error = e;
|
|
}
|
|
|
|
const promise = isPromise(value);
|
|
|
|
if (promise) {
|
|
return value
|
|
.then(res => ({ resolve: res, promise: true }))
|
|
.catch(rej => ({ rejection: rej, promise: true }));
|
|
} else {
|
|
return Promise.resolve({
|
|
...(!error && {value}),
|
|
...(error && {error}),
|
|
promise: false,
|
|
});
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = caller;
|
|
|