0

I'm struggling to wrap my head around the concept of async and promises in js. I can't figure out why the code below doesn't print anything on my console.

I'm assuming it's because the code inside my Promise is not asynchronous, but isn't that the point of a promise: to make something synchronous become asynchronous?

If that's not the case, how could I truly "transform" a sync code into async without using any built in js functions (setTimeOut,etc)?

function countdown(seconds) {
    return new Promise(function(resolve, reject) {
        for (let i = seconds; i >= 0; i--) {
            if (i > 0) console.log(i + '...');
            else resolve(console.log("GO!"));
        }
    }
};
count = countdown(5).then(() => console.log('Completed'), (err) => console.log(err.message));
4
  • "isn't that the point of a promise: to make something synchronous become asynchronous?" No, absolutely not. The point of promises is to make dealing with things that already are asynchronous easier, by treating them as returnable values. In your example, you have a synchronous loop, and there is zero reason to use a promise here. Commented Feb 14, 2017 at 19:42
  • I can't figure out why the code below doesn't print anything on my console - what? not even an error about the missing ) ? use a better browser Commented Feb 15, 2017 at 0:17
  • by it's nature, javascript is synchronous (waits for howls of protest to abate) ... any (non native) function that is asynchronous is due to that function directly, or indirectly calling one of the "native" functions that are asyncrhonous - therefore - the only way to transform some code from synchronous to asynchronous is to use one of the many "native" functions that are asynchronous in nature (again, either directly, or indirectly via other functions that eventually will have to call one of these asynchronous functions directly) Commented Feb 15, 2017 at 0:25
  • I see. It's getting more clear to me now. Thanks! Commented Feb 15, 2017 at 0:27

2 Answers 2

2

how could I truly "transform" a sync code into async without using any built in js functions (setTimeOut,etc)?

By it's nature, javascript code is synchronous (waits for howls of protest to abate) ...

Every (non-native) function that is asynchronous is due to that function, either

  1. directly calling one of those native asynchronous functions, or
  2. calling other functions that call functions etc that eventually call one of these asynchronous functions directly

the only way to transform some code from synchronous to asynchronous is to use one of the many "native" functions that are asynchronous in nature (again, either directly, or indirectly via other functions that eventually will have to call one of these asynchronous functions directly)

Sign up to request clarification or add additional context in comments.

Comments

2

It is missing ) , and it works now after adding that parenthesis .. Run snippet to check

function countdown(seconds) {
    return new Promise(function(resolve, reject) {
        for (let i = seconds; i >= 0; i--) {
            if (i > 0) console.log(i + '...');
            else resolve(console.log("GO!"));
        }
    }) // <---⚠️ I mean this parenthesis
};
count = countdown(5).then(() => console.log('Completed'), (err) => console.log(err.message));

2 Comments

I am really glad for you .. This is good to write funny answer .
why would you advocate such ridiculous code by providing an answer :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.