diff --git a/filenames.gypi b/filenames.gypi index cc33d9a81a..14d01270f0 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -42,6 +42,7 @@ 'lib/common/api/crash-reporter.js', 'lib/common/api/deprecate.js', 'lib/common/api/deprecations.js', + 'lib/common/api/is-promise.js', 'lib/common/api/exports/electron.js', 'lib/common/api/native-image.js', 'lib/common/api/shell.js', diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 7ad6d2c22a..fe264cceee 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -2,7 +2,7 @@ const electron = require('electron') const v8Util = process.atomBinding('v8_util') -const {ipcMain} = electron +const {ipcMain, isPromise} = electron const objectsRegistry = require('./objects-registry') @@ -65,7 +65,7 @@ let valueToMeta = function (sender, value, optimizeSimpleObject = false) { meta.type = 'error' } else if (value instanceof Date) { meta.type = 'date' - } else if (value.constructor != null && value.constructor.name === 'Promise') { + } else if (isPromise(value)) { meta.type = 'promise' } else if (value.hasOwnProperty('callee') && value.length != null) { // Treat the arguments object as array. diff --git a/lib/common/api/exports/electron.js b/lib/common/api/exports/electron.js index 1e9c2d71f3..efa44d452c 100644 --- a/lib/common/api/exports/electron.js +++ b/lib/common/api/exports/electron.js @@ -43,6 +43,11 @@ exports.defineProperties = function (exports) { get: function () { return require('../deprecations') } + }, + isPromise: { + get: function () { + return require('../is-promise') + } } }) } diff --git a/lib/common/api/is-promise.js b/lib/common/api/is-promise.js new file mode 100644 index 0000000000..d6115a1455 --- /dev/null +++ b/lib/common/api/is-promise.js @@ -0,0 +1,14 @@ +'use strict' + +module.exports = function isPromise (val) { + return ( + val && + val.then && + val.then instanceof Function && + val.constructor && + val.constructor.reject && + val.constructor.reject instanceof Function && + val.constructor.resolve && + val.constructor.resolve instanceof Function + ) +} diff --git a/lib/renderer/api/remote.js b/lib/renderer/api/remote.js index d3fdec25ae..b5c9fe3a0e 100644 --- a/lib/renderer/api/remote.js +++ b/lib/renderer/api/remote.js @@ -1,7 +1,7 @@ 'use strict' const v8Util = process.atomBinding('v8_util') -const {ipcRenderer, CallbacksRegistry} = require('electron') +const {ipcRenderer, isPromise, CallbacksRegistry} = require('electron') const callbacksRegistry = new CallbacksRegistry() @@ -44,7 +44,7 @@ var wrapArgs = function (args, visited) { value: value.getTime() } } else if ((value != null) && typeof value === 'object') { - if (value.constructor != null && value.constructor.name === 'Promise') { + if (isPromise(value)) { return { type: 'promise', then: valueToMeta(function (onFulfilled, onRejected) {