Skip to content

Resolving one promise from within another uses wrong storage #83

@watson

Description

@watson

I ran into an issue when resolving a promise from within the then callback of another promise. The issue can be recreated using the (over)simplified code below:

require('async-listener')

var cls = {n: 0}

process.addAsyncListener({
  create: function () {
    return {n: cls.n}
  },
  before: function (context, storage) {
    cls.n = storage.n
  }
})

var resolve
cls.n = 1
Promise.resolve().then(function then1 () {
  process._rawDebug('then1:', cls.n)
  resolve()
})

cls.n = 2
new Promise(function (_resolve) {
  resolve = _resolve
}).then(function then2 () {
  process._rawDebug('then2:', cls.n)
})

I expect the output of this to be:

then1: 1
then2: 2

But instead it's:

then1: 1
then2: 1

If the two promises are resolved individually it works as expected. But for some reason, the storage argument in the before hook of the then1 (edit: meant then2) callback is {n: 1} when the 2nd promise is resolved from within the then1 callback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions