window.onmessage = ...
window.postMessage('1', '*');
window.postMessage('2', '*');
Does postMessage (http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#queue-a-task) guarantee the order of events?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t know, although the wording of the spec seems to suggest it doesn’t make such guarantees (which is surprising). It’s clear that once the
MessageEventis added to thetask queueon the receiving end, then it’s order is maintained, although theMessageEventcreation and dispatch are asynchronous to the originalpostMessagecall, so theoretically it appears that you could have the following situation:main thread:
If the thread management system allowed the second
postMessageto execute before the first thread managed to dispatch theMessageEvent, and for whatever unlucky reason allowed that newer thread to execute (a diluted priority inversion), again before the first managed to dispatch, then you would indeed receive those messages in the reverse order.Although there might be some other place in the spec that provides more context for these asynchronous executions and rules out this case – I couldn’t find it.