From ec4956a379f2007dca97ab90ad081086fc928100 Mon Sep 17 00:00:00 2001 From: David Cormier Date: Sun, 27 Oct 2013 11:28:34 -0400 Subject: [PATCH] Implement doSendAllWithCallback doSendAllWithCallback sends all the messages of the list and returns a promise that is resolved when all the individual promises are resolved. Useful for avoiding long chains of .then().then().then() .. --- js/websockets.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/js/websockets.js b/js/websockets.js index 5a731cc..e68238e 100644 --- a/js/websockets.js +++ b/js/websockets.js @@ -212,6 +212,23 @@ weechat.factory('connection', ['$q', '$rootScope', '$log', '$store', 'handlers', return cb.promise; } + + /* + * Send all messages to the websocket and returns a promise that is resolved + * when all message are resolved. + * + * @param messages list of messages + * @returns a promise + */ + var doSendAllWithCallback = function(messages) { + var promises = []; + for(i in messages) { + var cb = createCallback(messages[i]); + promises.push(cb.promise); + }; + return $q.all(promises); + }; + // Sanitizes messages to be sent to the weechat relay var doSend = function(message) { msgs = message.replace(/[\r\n]+$/g, "").split("\n");