question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

thrown:undefined when using settimout in promise

See original GitHub issue

did 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:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
SimenBcommented, Mar 14, 2021

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

1reaction
nancy-Gaocommented, Jul 1, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found