From aabc102abbfe6a60ccafddd11db50f49eaaa29f0 Mon Sep 17 00:00:00 2001 From: Steve Purcell Date: Tue, 27 May 2014 17:37:07 +0100 Subject: [PATCH] Return results of sync promises in the same order they were given --- mithril.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mithril.js b/mithril.js index 1234d759..8ad8ef58 100644 --- a/mithril.js +++ b/mithril.js @@ -301,7 +301,7 @@ Mithril = m = new function app(window) { currentRoute = arguments[0] var querystring = typeof arguments[1] == "object" ? buildQueryString(arguments[1]) : null if (querystring) currentRoute += (currentRoute.indexOf("?") === -1 ? "?" : "&") + querystring - + var shouldReplaceHistoryEntry = (arguments.length == 3 ? arguments[2] : arguments[1]) === true if (window.history.pushState) { @@ -427,11 +427,11 @@ Mithril = m = new function app(window) { } m.sync = function(args) { var method = "resolve" - function synchronizer(resolved) { + function synchronizer(pos, resolved) { return function(value) { - results.push(value) + results[pos] = value if (!resolved) method = "reject" - if (results.length == args.length) { + if (--outstanding == 0) { deferred.promise(results) deferred[method](results) } @@ -440,9 +440,10 @@ Mithril = m = new function app(window) { } var deferred = m.deferred() - var results = [] + var outstanding = args.length + var results = new Array(outstanding) for (var i = 0; i < args.length; i++) { - args[i].then(synchronizer(true), synchronizer(false)) + args[i].then(synchronizer(i, true), synchronizer(false)) } return deferred.promise }