
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
flush-promises
Advanced tools
The 'flush-promises' npm package is a utility designed to help with testing asynchronous code in JavaScript. It allows you to flush all pending resolved promises, making it easier to test code that relies on asynchronous operations.
Flushing Promises
This feature allows you to flush all pending promises, ensuring that all asynchronous operations have completed. This is particularly useful in testing scenarios where you need to wait for all promises to resolve before making assertions.
const flushPromises = require('flush-promises');
async function asyncFunction() {
return 'resolved';
}
asyncFunction().then(result => console.log(result));
flushPromises().then(() => {
console.log('All promises have been flushed');
});
Jest is a popular testing framework that includes utilities for handling asynchronous code. It provides functions like `waitFor` and `waitForNextUpdate` which can be used to wait for promises to resolve. Compared to 'flush-promises', Jest offers a more comprehensive testing solution but may be overkill if you only need to flush promises.
Sinon is a library for creating spies, mocks, and stubs in JavaScript. It includes utilities for dealing with asynchronous code, such as `sinon.useFakeTimers` and `sinon.clock.tick`. While Sinon is more focused on mocking and spying, it can also be used to manage asynchronous operations, making it a versatile alternative to 'flush-promises'.
The 'p-settle' package is used to settle promises concurrently and get their results. It is useful for handling multiple promises and ensuring they all resolve. While 'p-settle' is more focused on managing multiple promises, 'flush-promises' is specifically designed for flushing all pending promises in a testing context.
Flush all pending resolved promise handlers. Useful in tests.
const flushPromises = require('flush-promises');
test('flushPromises', async () => {
let a;
let b;
Promise.resolve().then(() => {
a = 1;
}).then(() => {
b = 2;
})
await flushPromises();
expect(a).toBe(1);
expect(b).toBe(2);
});
import * as flushPromises from "flush-promises";
test("flushPromises", async () => {
let a;
let b;
Promise.resolve().then(() => {
a = 1;
}).then(() => {
b = 2;
});
await flushPromises();
expect(a).toBe(1);
expect(b).toBe(2);
});
FAQs
Flush promises in tests
The npm package flush-promises receives a total of 228,008 weekly downloads. As such, flush-promises popularity was classified as popular.
We found that flush-promises demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.