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));
code11
  • 1,986
  • 5
  • 29
  • 37
user3682983
  • 177
  • 2
  • 14
  • "*isn't that the point of a promise: to make something synchronous become asynchronous?*" No, absolutely not. The [point of promises](http://stackoverflow.com/a/22562045/1048572) 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. – Bergi Feb 14 '17 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 – Jaromanda X Feb 15 '17 at 00: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) – Jaromanda X Feb 15 '17 at 00:25
  • I see. It's getting more clear to me now. Thanks! – user3682983 Feb 15 '17 at 00:27

2 Answers2

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));
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
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)

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87