Skip to content

Instantly share code, notes, and snippets.

@jimmy-collazos
Created July 19, 2018 12:28
Show Gist options
  • Save jimmy-collazos/824272c67060fab182d8cf597d1a1659 to your computer and use it in GitHub Desktop.
Save jimmy-collazos/824272c67060fab182d8cf597d1a1659 to your computer and use it in GitHub Desktop.
Helper Promise - isPending()
/**
* Check if Promise is pendding to resolve. Use in Asyn-Await
*
* Example;
*
* const { isPending } = require('promise.helper')
* it('Check if promise is pending', async function () {
* const promise = load(path);
* console.assert(await isPending(promise))
* await promise;
* console.assert(!(await isPending(promise)))
* })
*/
exports.isPending = (p, _t = {}) => Promise.race([p, _t]).then(v => (v === _t));
/**
* Check if Promise is pendding to resolve. Use in Asyn-Await
*
* Example;
*
* import { isPending } from 'promise.helper'
* it('Check if promise is pending', async function () {
* const promise = load(path);
* console.assert(await isPending(promise))
* await promise;
* console.assert(!(await isPending(promise)))
* })
*/
export const isPending = (p, _t = {}) => Promise.race([p, _t]).then(v => (v === _t));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment