brummelte / sleep-promise Goto Github PK
View Code? Open in Web Editor NEWResolves a promise after a specified delay.
License: MIT License
Resolves a promise after a specified delay.
License: MIT License
Hi again @brummelte and thanks for your efforts to move the project to the next level!
I'm writing to report that sleep-promise
v4.1 does not work in a TypeScript project. This is because Node still imports a js file rather than an mjs
, but that file does not have a default
export. Now the library is not usable because when I type import * as sleep from "sleep-promise"
, I get a TypeScript compiler error, expectedly coming from index.d.ts
, but when I try import sleep as "sleep-promise"
and get cool TS hints, this exception shows up:
TypeError: sleep_promise_1.default is not a function
I guess you'll need to export sleep
function twice to make bundle.js
compatible with both require
and import
:
module.exports = sleep;
module.exports.default = module.exports;
This is what chalk
does at least, but they are implementing both flavours of exports manually. Given that your source is in es6 now, perhaps, there is some rollup option that would do the trick?
Rolling back to v2 for now π
An unnecessary dependency on core-js
was removed in #27 in October. However, the latest published release (9.0.0) still includes it. Itβd be great if you could release 9.0.1 at some point, which would not includecore-js
. This may resolve certain issues related to dependency hoisting and resolution. Cheers!
Please clarify what the units are for the timeout
argument by adding a comment.
sinon is a popular stubbing framework for JavaScript unit tests. sinon.useFakeTimers()
replaces the global setTimeout()
method with its own, allowing unit tests to instantly move time forward instead of waiting for time to pass.
Because this project calls the global setTimeout()
method, it will not work if sinon.useFakeTimers()
is called.
A simple solution would be to store the global setTimeout()
method before sinon replaces it:
sleep-promise/lib/index.js
const setTimeout = global.setTimeout;
// sleep() implementation...
someProject/test.js
const sleep = require('sleep-promise');
const clock = sinon.useFakeTimers();
// Unit test code...
If you're ok with this from a design perspective, I'd be happy to open a PR.
There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.
Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
@semantic-release/commit-analyzer
, @semantic-release/release-notes-generator
, semantic-release
).github/workflows/rebase.yml
actions/checkout v4
cirrus-actions/rebase 1.3.1
.github/workflows/release.yml
actions/checkout v4
actions/setup-node v4
.yarn/sdks/eslint/package.json
.yarn/sdks/typescript/package.json
package.json
@babel/cli ^7.24.7
@babel/core ^7.24.7
@babel/plugin-transform-runtime ^7.24.7
@babel/preset-env ^7.24.7
@brummelte/eslint-config ^9.2.29
@semantic-release/changelog ^6.0.3
@semantic-release/commit-analyzer ^12.0.0
@semantic-release/git ^10.0.1
@semantic-release/github ^10.0.6
@semantic-release/npm ^12.0.1
@semantic-release/release-notes-generator ^13.0.0
babel-jest ^29.7.0
copyfiles ^2.4.1
coveralls ^3.1.1
eslint ^8.57.0
jest ^29.7.0
jest-junit ^16.0.0
rimraf ^5.0.7
rollup ^2.79.1
rollup-plugin-babel ^4.4.0
rollup-plugin-node-resolve ^5.2.0
rollup-plugin-terser ^7.0.2
semantic-release ^23.1.1
typescript ^5.4.5
I imagine a method to delay a promise chain would be extremely useful.
Something to allow:
Promise.resolve("hello world")
.then(sleep(5000))
.then(x => x.toUpperCase())
.then(x => console.log(x))
this can be useful to control an animation or display a notification
sendEmail("[email protected]")
.then(showNotification)
.then(sleep(200))
.then(id => hideNotification(id))
what do you think?
I'm using this library for handling timeout stuffs or something else like:
await Promise.all([
triggerPromise(),
Promise.race([
eventPromise.then(...), // event might be invoked or not
sleep(1000).then(() => Promise.reject('timeout exceeded of 1000ms while ...')), // timeout
]),
]);
(feature request)
In the case above all things are good, however I have faced a situation where the sleep promise should be cancelled to continue, as like:
const timeout = sleep(1000).then(() => Promise.reject(...));
await Promise.all([
triggerPromise(),
Promise.race([
eventPromise.then(() => {
timeout.cancel(); // here
return operationPromiseThatTakesMoreThanTimeout();
}),
timeout,
]);
]);
(current solution)
Anyway, this goal can be reached by writing code like as follows:
const timeout = sleep(1000).then(() => Promise.reject(...));
await Promise.all([
triggerPromise(),
new Promise((resolve, reject) => {
let handled = false;
eventPromise.then(() => {
handled = true;
operationPromiseThatTakesMoreThanTimeout().then(resolve);
});
sleep(1000).then(() => { // or `setTimeout`
if (handled) {
return;
}
reject(...);
});
}),
]);
(or similar)
but you may know this is bad idea/style, just because if sleep-promise supports promise cancellation for the case like this.
The README example shows
import sleep from 'sleep-promise';
This is a default import. But there's no default export on this module:
> require('sleep-promise').default
undefined
It'd be great if the library provided TypeScript typings! Otherwise, this warning is shown next to import * as sleep from "sleep-promise";
:
Could not find a declaration file for module 'sleep-promise'.
'/path/to/project/node_modules/sleep-promise/lib/index.js' implicitly has an 'any' type.
Try `npm install @types/sleep-promise` if it exists or add a new declaration
(.d.ts) file containing `declare module 'sleep-promise';`
I can submit a PR if this idea gets approved.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.