
Security Fundamentals
Obfuscation 101: Unmasking the Tricks Behind Malicious Code
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
poll-until-promise
Advanced tools
Wait until the executed promise resolved to a true value, Execute it every x milliseconds and stop after y milliseconds.
npm install poll-until-promise
const { waitFor } = require('poll-until-promise');
waitFor(() => fetch('/get-data'), { interval: 100 })
// Tries every 100ms (from the last failure)
.then(value => console.log('Yey', value))
.catch(err => console.error(err));
import { waitFor, AbortError } from 'poll-until-promise';
async function waitForDomElement(cssSelector = 'div') {
try {
const element = await waitFor(() => {
const element = window.document.querySelector(cssSelector);
if (!element) throw new Error(`failed to find element: ${cssSelector}`);
return element;
}, { timeout: 60_000 });
return element;
} catch (e) {
console.error('faled to find dom element:', e);
throw e;
}
}
async function retryFetch(path = '/get-data') {
try {
const data = await waitFor(async () => {
const res = await fetch(path);
// Stop immediately if the resource doesn't exist
if (res.status === 404) {
throw new AbortError(res.statusText);
}
return res.json();
}, { timeout: 60_000, interval: 1000 });
} catch (e) {
console.error('faled to fetch:', e);
throw e;
}
}
const { waitFor } = require('poll-until-promise');
waitFor(() => {
if (Math.random() >= 0.5) {
throw new Error('try again');
} else {
console.log('all good')
}
})
.then(() => console.log('Yey'))
.catch(err => console.error(err));
const { PollUntil } = require('poll-until-promise');
const later = Date.now() + 1000; // 1 seconds into the future
let pollUntilPromise = new PollUntil();
pollUntilPromise
.stopAfter(2 * 1000) // Stop trying after 2 seconds
.tryEvery(100) // Tries every 100ms (from the last failure)
.execute(() => {
return new Promise((resolve, reject) => {
if (+Date.now() >= later) {
return resolve(true); // Some truthy value
}
reject(false);
})
})
.then(value => console.log('Yey', value))
.catch(err => console.error(err));
const options = {
interval: 100,
backoffFactor: 1, // Exponential interval increase. Defaults to 1, which means no backoff
backoffMaxInterval: 250, // Sets a maximum interval when using backoff. Defaults to the timeout value
timeout: 1000,
stopOnFailure: false, // Ignores promise rejections
verbose: false,
message: 'Waiting for time to pass :)', // custom message to display on failure
maxAttempts: 5, // maximum attempts to make (default: no limit). Will still fail to wait if reaching timeout before attempts exhausted
};
let pollUntilPromise = new PollUntil(options);
pollUntilPromise.isResolved()
pollUntilPromise.isWaiting()
pollUntilPromise.getPromise().then(() => console.log('OMG'))
const PollUntil = require('poll-until-promise');
const later = Date.now() + 1000; // 1 seconds into the future
let pollUntilPromise = new PollUntil();
pollUntilPromise
.stopAfter(2 * 1000)
.tryEvery(100)
.execute(() => {
if (+Date.now() >= later) {
return true;
}
return false;
})
.then((value) => console.log('Yey', value))
.catch((err) => console.error(err));
FAQs
Try repeatedly for a promise to be resolved
The npm package poll-until-promise receives a total of 7,751 weekly downloads. As such, poll-until-promise popularity was classified as popular.
We found that poll-until-promise 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 Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.
Security News
Join Socket for exclusive networking events, rooftop gatherings, and one-on-one meetings during BSidesSF and RSA 2025 in San Francisco.
Security News
Biome's v2.0 beta introduces custom plugins, domain-specific linting, and type-aware rules while laying groundwork for HTML support and embedded language features in 2025.