Do we need "await" when using ".then"?

#17 · closed · 4 comments

View on GitHub ↗

FarazzShaikh

Do we need the `await` when using `.then` when we call `executeCommand()` in for example: https://github.com/Andrew-Colman/sync-fork/blob/d44269367f482ac97efc2374b52e94bd8fc8a816/lib/utils.js#L46-L52 From what i have read its ether `await` or `then...catch`

Comments

Andrew-Colman

yes, we're **await**ing for the value **then** using it all inside a **try / catch** ``` catch (err) { errorHandler(err); //invokes the custom error handler that gives custom messages } ``` if we remove it: ``` cmd.then( cmd.then( cmd.then( cmd.then( ).catch(e) ).catch(e) ).catch(e) ).catch(e) ``` ![then](https://blog.avenuecode.com/hubfs/Screen%20Shot%202019-01-09%20at%202.28.27%20PM.png) >https://blog.avenuecode.com/callback-hell-promises-and-async/await

FarazzShaikh

But cant we just rewrite the example i showed as: ```js try { // Removed then, reads more like synchronous code const { stdout } = await executeCommand(verifyCommand) if (!stdout.match('git version')) { return log(solutions.gitNotInstalled) } else { return log(solutions.started) } } catch (err) { console.error(err) } ```

Andrew-Colman

```js try { // Removed then, reads more like synchronous code const { stdout } = await executeCommand(oneCommand) if (!stdout.match('git version')) { return log(solutions.gitNotInstalled) } else { return log(solutions.started) } const { stdout } = await executeCommand(twoCommand) if (!stdout.match('..............')) { return log(solutions......) } else { return log(solutions.....) } const { stdout } = await executeCommand(threeCommand) if (!stdout.match('..............')) { return log(solutions......) } else { return log(solutions.....) } const { stdout } = await executeCommand(fourCommand) if (!stdout.match('..............')) { return log(solutions......) } else { return log(solutions.....) } const { stdout } = await executeCommand(fiveCommand) if (!stdout.match('..............')) { return log(solutions......) } else { return log(solutions.....) } } catch (err) { console.error(err) } ``` - will be a bit confusing in terms of scope - and re-creating the variables in the same scope of the main function **syncFork**() - `const stdout` (I'm trying to stick close as possible with immutability) - ```js executeCommand(nameCommand) { {stdout} //<- this stdout lives only in this scope, easy debuggable with a console.log and Stop(); } ```

FarazzShaikh

Hmm yeah makes sense. Thanks for the detailed reply