thrown:undefined when using settimout in promise
See original GitHub issuedid anybody get the same error when i use promise and setTimeout to mock delay case, but it doesn’t work. I have no clue to resolve it. system: node v12.14.1 npm v6.13.4 jest 24.9.0
// index.js
export const fn = () => {
return new Promise(resolve => {
setTimeout(() => {
resolve('it didnt work');
}, 0);
});
}
// index.test.js
const { fn } = require('./a');
describe('aa', () => {
test('aaa', async () => {
const data = await fn();
return expect(data).toEqual('it didnt work');
});
});
=>result error:
thrown: undefined
at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why can I not call resolve() for Promises in SetTimeout ...
This means "immediately call resolve('done') and pass its result into settimeout". the return value from resolve is undefined , so undefined ...
Read more >Promise.prototype.then() - JavaScript - MDN Web Docs
The below snippet simulates asynchronous code with the setTimeout function. The value returned from then() is resolved in the same way as ......
Read more >Wait for Promise to finish executing in Javascript Function
This code example is not complete, but the fundamental issue appears to be mixing synchronous and asynchronous code.
Read more >The 10 Most Common JavaScript Issues Developers Face
As a result, the anonymous function being passed to setTimeout() is being ... call would either output undefined or throw an error, you...
Read more >How can I add a timeout to a promise in JavaScript?
const awaitTimeout = delay => new Promise(resolve => setTimeout(resolve, delay)); awaitTimeout(300).then(() => console.log( ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
Thank you so much. I resovled it! The reason of why Promise doesn’t work is that the project imports a file who includes Promise and throws error at the entry file.