Skip to content

A better way to detect native Promise? #56

@watson

Description

@watson

Right now the presence of native Promise is detected based on how the code is currently instrumented and how it behaves when used:

async-listener/index.js

Lines 260 to 274 in 0c2e2ec

if (instrumentPromise) {
// shoult not use any methods that have already been wrapped
var promiseListener = process.addAsyncListener({
create: function create() {
instrumentPromise = false;
}
});
// should not resolve synchronously
global.Promise.resolve(true).then(function notSync() {
instrumentPromise = false;
});
process.removeAsyncListener(promiseListener);
}

I was thinking that there might be a better way of detecting if the current Promise implementation is native or not - simply by checking if global.Promise is a native function or not.

The simplified approach to check this would rely on the result of String(global.Promise) which produces the following string:

function Promise() { [native code] }

The presence of { [native code] } gives it away.

But as the blog post Detect if a Function is Native Code with JavaScript and the is-native module shows, there are some edge cases that should be checked.

So my question is, could we simply not require is-native or something similar and replace line 257-274 with the following snippet?

var isNative = require('is-native')
var instrumentPromise = isNative(global.Promise)

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