From d7c8b4478b9d6945889f14a6301c695a08ed5ad0 Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Mon, 9 Nov 2015 20:13:27 -0800 Subject: [PATCH] Make InteractionManager.runAfterInteractions() return a Promise Summary: In accordance with the unwritten rule that any API that takes a callback should also return a promise, I've changed `InteractionManager.runAfterInteractions()` to do just that. ```js InteractionManager.runAfterInteractions(() => { ... }); ``` can become ```js InteractionManager.runAfterInteractions().then(() => { ... }); ``` (but doesn't have to). Most importantly, though, this change enables code like ```js doSomeUIStuff(); await InteractionManager.runAfterInteractions(); doSomeNonUIStuff(); ``` which is nice. Note: Because returning a `Promise` means the callback argument is now optional, the behaviour of the API is slightly changed, though not in a backwards-incompatible way (unless a consumer is in some way relying on the exception, but that would be insane). Closes https://github.com/facebook/react-native/pull/3788 Reviewed By: vjeux Differential Revision: D2634693 Pulled By: josephsavona fb-gh-sync-id: 7315120963be23cf69d777e940b2750d32ae47a8 --- Libraries/Interaction/InteractionManager.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Libraries/Interaction/InteractionManager.js b/Libraries/Interaction/InteractionManager.js index 37625978a..fd4ab67c3 100644 --- a/Libraries/Interaction/InteractionManager.js +++ b/Libraries/Interaction/InteractionManager.js @@ -73,13 +73,14 @@ var InteractionManager = { /** * Schedule a function to run after all interactions have completed. */ - runAfterInteractions(callback: Function) { - invariant( - typeof callback === 'function', - 'Must specify a function to schedule.' - ); - scheduleUpdate(); - _queue.push(callback); + runAfterInteractions(callback: ?Function): Promise { + return new Promise(resolve => { + scheduleUpdate(); + if (callback) { + _queue.push(callback); + } + _queue.push(resolve); + }); }, /**