Skip to content

Commit 3580096

Browse files
motiz88facebook-github-bot
authored andcommitted
Deprecate nonstandard Promise.prototype.done
Summary: Deprecates the nonstandard `Promise.prototype.done` method. This also removes one call site within React Native itself that relied on this method. As part of this we are also removing React Native's custom Flow definition for `Promise` in favour of the standard one built into Flow. This will flag uses of `done` as type errors for anyone using the default app template's `.flowconfig`. In a future release of React Native, we will remove the `done` method from the built-in `Promise` polyfill. Changelog: [General][Deprecated] - Deprecate the Promise.prototype.done method and log a warning when it's called in development. Reviewed By: yungsters Differential Revision: D34222667 fbshipit-source-id: 4b9708ac20c45b3966fdb93e883ab7f8d80017c1
1 parent 062c1f7 commit 3580096

File tree

4 files changed

+37
-62
lines changed

4 files changed

+37
-62
lines changed

Libraries/Core/polyfillPromise.js

+32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
const {polyfillGlobal} = require('../Utilities/PolyfillFunctions');
14+
const warnOnce = require('../Utilities/warnOnce');
1415

1516
/**
1617
* Set up Promise. The native Promise implementation throws the following error:
@@ -36,3 +37,34 @@ if (global?.HermesInternal?.hasPromise?.()) {
3637
} else {
3738
polyfillGlobal('Promise', () => require('../Promise'));
3839
}
40+
41+
if (__DEV__) {
42+
// $FlowFixMe
43+
const done = Promise.prototype.done;
44+
if (done != null) {
45+
let depth = 0;
46+
/* eslint-disable no-extend-native */
47+
// $FlowFixMe
48+
Promise.prototype.done = function () {
49+
++depth;
50+
try {
51+
// Avoid infinite recursion if done() happens to be triggered by warnOnce.
52+
if (depth === 1) {
53+
// Warn once per unique call stack. Not super efficient, but we're in
54+
// __DEV__ and .done() calls are rare to begin with.
55+
const key = new Error().stack;
56+
warnOnce(
57+
key,
58+
'Promise.prototype.done(): This nonstandard polyfill ' +
59+
'has been deprecated and will be removed in a future release. ' +
60+
'Please instead use `.then()`.',
61+
);
62+
}
63+
} finally {
64+
--depth;
65+
}
66+
return done.apply(this, arguments);
67+
};
68+
/* eslint-enable no-extend-native */
69+
}
70+
}

Libraries/Interaction/InteractionManager.js

-11
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ const InteractionManager = {
9191
onFulfill?: ?(void) => ?(Promise<U> | U),
9292
onReject?: ?(error: mixed) => ?(Promise<U> | U),
9393
) => Promise<U>,
94-
done: () => void,
9594
cancel: () => void,
9695
...
9796
} {
@@ -110,16 +109,6 @@ const InteractionManager = {
110109
return {
111110
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
112111
then: promise.then.bind(promise),
113-
done: (...args) => {
114-
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
115-
if (promise.done) {
116-
return promise.done(...args);
117-
} else {
118-
console.warn(
119-
'Tried to call done when not supported by current Promise implementation.',
120-
);
121-
}
122-
},
123112
cancel: function () {
124113
_taskQueue.cancelTasks(tasks);
125114
},

Libraries/Interaction/TaskQueue.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ class TaskQueue {
171171
this.hasTasksToProcess() && this._onMoreTasks();
172172
})
173173
.catch(ex => {
174-
ex.message = `TaskQueue: Error resolving Promise in task ${task.name}: ${ex.message}`;
175-
throw ex;
176-
})
177-
.done();
174+
setTimeout(() => {
175+
ex.message = `TaskQueue: Error resolving Promise in task ${task.name}: ${ex.message}`;
176+
throw ex;
177+
}, 0);
178+
});
178179
}
179180
}
180181

flow/Promise.js

-47
This file was deleted.

0 commit comments

Comments
 (0)