faked-promise
Advanced tools
Comparing version 1.0.1 to 2.0.0
13
index.js
module.exports = function fakePromise () { | ||
// eslint-disable-next-line | ||
return new Promise(outerResolve => { | ||
const promise = new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
outerResolve([promise, resolve, reject]) | ||
}, 0) | ||
}) | ||
let res, rej | ||
// the executor is guaranteed to get executed right away | ||
const promise = new Promise((resolve, reject) => { | ||
res = resolve | ||
rej = reject | ||
}) | ||
return [promise, res, rej] | ||
} |
{ | ||
"name": "faked-promise", | ||
"version": "1.0.1", | ||
"description": "Create a fake promise that can be resolved and rejected programatically", | ||
"version": "2.0.0", | ||
"description": "Create a promise that can be resolved and rejected outside of it", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "author": { |
# faked-promise [![Build Status](https://img.shields.io/circleci/project/posva/faked-promise/master.svg)](https://circleci.com/gh/posva/faked-promise) [![npm package](https://img.shields.io/npm/v/faked-promise.svg)](https://www.npmjs.com/package/faked-promise) [![coverage](https://img.shields.io/codecov/c/github/posva/faked-promise.svg)](https://codecov.io/github/posva/faked-promise) [![donate](https://img.shields.io/badge/donate-%E2%99%A5-ff69b4.svg)](https://github.com/posva/donate) | ||
> Create a fake promise that can be resolved and rejected programatically | ||
> Create a promise that can be resolved and rejected programatically outside of it | ||
It basically extracts the `resolve` and `reject` callbacks so you can call them whenever you need. | ||
⚠️ The promise can only be resolved once, it's still a regular Promise after all. | ||
## Installation | ||
@@ -13,3 +16,3 @@ | ||
faked-promise exports one single function that returns a promise. This promise resolves into an array with 3 elements: | ||
faked-promise exports one single function that takes no arguments and returns an array with 3 elements: | ||
@@ -20,3 +23,3 @@ - A real promise | ||
Meaning you can control exactly when the promise is resolved or rejected: | ||
Meaning you can control exactly when the promise is resolved or rejected: | ||
@@ -26,3 +29,3 @@ ```js | ||
const [promise, resolve, reject] = await fakePromise() | ||
const [promise, resolve, reject] = fakePromise() | ||
promise.then(() => {}) | ||
@@ -29,0 +32,0 @@ resolve('any value') |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
3881
35
9