Search code examples
javascriptes6-promise

How to work properly with promises in JavaScript after closing the browser?


I am using firebase or pouchsb / couchdb + vuejs / quasar. My architecture does not imply a server layer as such.

There is a task of the following nature. Let's say that at 14:00 the user creates an order that must be executed at 15:00. What if the browser is closed and then reopened at 14:58?

How to make a promise to be fulfilled after closing and reopening the browser?


Solution

  • You can't. When the browser is closed (or the page existed) the JS environment goes away and all promises disappear. You need to take a different approach.

    A typical approach would be to store the data about the order in (for example) localStorage and then:

    • Delete it when the order is executed
    • Read it when the page is loaded and:
      • put pending orders back on the queue
      • handle out-of-date orders however you like (e.g. display an error or submit them late)