A model for reasoning about JavaScript promises

作者:Magnus, Madsen; Ondřej, Lhoták; Frank, Tip
来源:Proceedings of the ACM on Programming Languages, 2017, 1(OOPSLA): 1-24.
DOI:10.1145/3133910

摘要

<jats:p>In JavaScript programs, asynchrony arises in situations such as web-based user-interfaces, communicating with servers through HTTP requests, and non-blocking I/O. Event-based programming is the most popular approach for managing asynchrony, but suffers from problems such as lost events and event races, and results in code that is hard to understand and debug. Recently, ECMAScript 6 has added support for promises, an alternative mechanism for managing asynchrony that enables programmers to chain asynchronous computations while supporting proper error handling. However, promises are complex and error-prone in their own right, so programmers would benefit from techniques that can reason about the correctness of promise-based code.</jats:p> <jats:p> Since the ECMAScript 6 specification is informal and intended for implementers of JavaScript engines, it does not provide a suitable basis for formal reasoning. This paper presents λ <jats:sub> <jats:italic>p</jats:italic> </jats:sub> , a core calculus that captures the essence of ECMAScript 6 promises. Based on λ <jats:sub> <jats:italic>p</jats:italic> </jats:sub> , we introduce the promise graph, a program representation that can assist programmers with debugging of promise-based code. We then report on a case study in which we investigate how the promise graph can be helpful for debugging errors related to promises in code fragments posted to the StackOverflow website. </jats:p>