Skip to content
This repository has been archived by the owner on Aug 29, 2021. It is now read-only.

Feedback: top level await removes bootstrap hazards #93

Closed
mcollina opened this issue May 31, 2019 · 3 comments
Closed

Feedback: top level await removes bootstrap hazards #93

mcollina opened this issue May 31, 2019 · 3 comments

Comments

@mcollina
Copy link

In Node.js, a lot of inexperienced developers do the following:

const Database = require('database')
Database.connect().then(function(client) {
  module.exports.client = client
})

then in another file:

const db = require('./db')
http.createServer(function(req, res) {
  db.findOne('myData').then(function(data) {
    res.send(JSON.stringify(data))
  }).catch(function(err) {
   res.statusCode = 500
   res.end(err.message)
  })
}).listen(3000)

This creates a bootstrap hazard between listening to a port and connecting to a database.

While the best practice is to bootstrap the server asynchronously, introducing top level await would provide a crude mechanism to avoid those hazards. Developers could then easily migrate to an async bootstrap as a more advanced technique.

@mcollina
Copy link
Author

To articulate, what would be possible to do with top level await is the following:

import Database from 'database'

const client = await Database.connect()

http.createServer(function(req, res) {
  db.findOne('myData').then(function(data) {
    res.send(JSON.stringify(data))
  }).catch(function(err) {
   res.statusCode = 500
   res.end(err.message)
  })
}).listen(3000)

@icodeforlove
Copy link

Would love to see this happen.

At the moment the only way I can accomplish this is with deasync. I would be very happy if i could remove this hack of a dependency.

@MylesBorins
Copy link
Member

As Top-Level Await has reached Stage 3 and is in the process of being implemented I'm going to close this issue in lieu of the tracking issue for the implementation across vendors #113

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants