{"action":"created","issue":{"url":"https://api.github.com/repos/w3ctag/promises-guide/issues/35","labels_url":"https://api.github.com/repos/w3ctag/promises-guide/issues/35/labels{/name}","comments_url":"https://api.github.com/repos/w3ctag/promises-guide/issues/35/comments","events_url":"https://api.github.com/repos/w3ctag/promises-guide/issues/35/events","html_url":"https://github.com/w3ctag/promises-guide/issues/35","id":55559327,"number":35,"title":"Shorthand for parallel steps","user":{"login":"jyasskin","id":83420,"avatar_url":"https://avatars.githubusercontent.com/u/83420?v=3","gravatar_id":"","url":"https://api.github.com/users/jyasskin","html_url":"https://github.com/jyasskin","followers_url":"https://api.github.com/users/jyasskin/followers","following_url":"https://api.github.com/users/jyasskin/following{/other_user}","gists_url":"https://api.github.com/users/jyasskin/gists{/gist_id}","starred_url":"https://api.github.com/users/jyasskin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jyasskin/subscriptions","organizations_url":"https://api.github.com/users/jyasskin/orgs","repos_url":"https://api.github.com/users/jyasskin/repos","events_url":"https://api.github.com/users/jyasskin/events{/privacy}","received_events_url":"https://api.github.com/users/jyasskin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-01-27T00:37:24Z","updated_at":"2015-01-27T16:30:20Z","closed_at":null,"body":"Lots of specs have algorithms that http://www.w3.org/2001/tag/doc/promises-guide#explicit-async-steps suggests be written as:\r\n\r\n1. Let _p_ be a new promise.\r\n1. Run the following steps in parallel:\r\n   1. Resolve _p_ with the meat of the algorithm\r\n1. Return _p_.\r\n\r\nSome existing algorithms, like http://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-encrypt, use the following pattern instead:\r\n\r\n1. Let _p_ be a new promise.\r\n1. Return _p_ and asynchronously perform the remaining steps.\r\n1. Resolve _p_ with the meat of the algorithm.\r\n\r\nA few existing algorithms, like https://html.spec.whatwg.org/multipage/webappapis.html#dom-createimagebitmap, shorten this further:\r\n\r\n1. Return a new Promise, but continue running these steps in parallel.\r\n1. Resolve the Promise with the meat of the algorithm.\r\n\r\nThe current recommendation requires 1-2 extra bullets for every algorithm and requires that the meat of the algorithm be indented one extra level, compared to the 2 alternatives.\r\n\r\nWould it be possible for the TAG's promises guide to endorse \"continue running these steps in parallel\" and the practice of leaving the Promise unnamed when there's only 1 Promise in the algorithm?"},"comment":{"url":"https://api.github.com/repos/w3ctag/promises-guide/issues/comments/71678276","html_url":"https://github.com/w3ctag/promises-guide/issues/35#issuecomment-71678276","issue_url":"https://api.github.com/repos/w3ctag/promises-guide/issues/35","id":71678276,"user":{"login":"jan-ivar","id":3136226,"avatar_url":"https://avatars.githubusercontent.com/u/3136226?v=3","gravatar_id":"","url":"https://api.github.com/users/jan-ivar","html_url":"https://github.com/jan-ivar","followers_url":"https://api.github.com/users/jan-ivar/followers","following_url":"https://api.github.com/users/jan-ivar/following{/other_user}","gists_url":"https://api.github.com/users/jan-ivar/gists{/gist_id}","starred_url":"https://api.github.com/users/jan-ivar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jan-ivar/subscriptions","organizations_url":"https://api.github.com/users/jan-ivar/orgs","repos_url":"https://api.github.com/users/jan-ivar/repos","events_url":"https://api.github.com/users/jan-ivar/events{/privacy}","received_events_url":"https://api.github.com/users/jan-ivar/received_events","type":"User","site_admin":false},"created_at":"2015-01-27T16:30:20Z","updated_at":"2015-01-27T16:30:20Z","body":"(My 2 cents) There's a sequence to things, and nothing happens after a return. return f(); first calls f() and *then* returns its result. - so I find the \"return and\" construct tightly coupled, confusing and inferior.\r\n\r\nI think what @domenic already has is outstandingly readable. It does several things nicely:\r\n1. It forks the main sequence at the right place.\r\n2. The indentation and the continuation of the main sequence together literally illustrate that a parallel sequence starts.\r\n3. The parallel sequence is bracketed by the creation and return of the promise made by it.\r\n4. It is loosely coupled.\r\n\r\nI think the last point is important, because imagine an algorithm that has other work to do before returning, or needs to start two parallel threads. I think those cases show that the \"return and\" pattern is a special case we can avoid."}}