unexpected
Version:
Extensible BDD assertion toolkit
158 lines (130 loc) • 8.85 kB
HTML
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
<link rel="stylesheet" href="../../static/normalize.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../static/main.css" type="text/css" media="screen" />
<link rel="shortcut icon" type="image/vnd.microsoft.icon" href="../../static/bug-icon-black.ico">
<title>promise</title>
</head>
<body class="sidebar-hidden has-sidebar">
<header>
<div class="logo-icon"></div>
<nav>
<ul>
<li class="menu-toggle-item">
<button class="menu-toggle" onclick="toggleSidebar()"></button>
</li>
<li class=""><a href="../..">Unexpected</a></li>
<li class=""><a href="../../assertions/any/to-be">Assertions</a></li>
<li class="active"><a href="../addAssertion">API</a></li>
<li class=""><a href="../../plugins">Plugins</a></li>
</ul>
</nav>
<div class="search" style="visibility: hidden">
<input id="search" placeholder="Search..." value="">
<div id="searchDropDown" class="dropDown">
<ul id="searchResults"></ul>
</div>
</div>
</header>
<section id="api">
<nav id="api-menu" class="sidebar js-remember-scroll-position">
<ul>
<li class="">
<a href="../addAssertion">addAssertion</a>
</li>
<li class="">
<a href="../addType">addType</a>
</li>
<li class="">
<a href="../async">async</a>
</li>
<li class="">
<a href="../clone">clone</a>
</li>
<li class="">
<a href="../expect">expect</a>
</li>
<li class="">
<a href="../fail">fail</a>
</li>
<li class="active">
<a href="">promise</a>
</li>
<li class="">
<a href="../promise-all">promise.all</a>
</li>
<li class="">
<a href="../promise-any">promise.any</a>
</li>
<li class="">
<a href="../promise-settle">promise.settle</a>
</li>
<li class="">
<a href="../toString">toString</a>
</li>
<li class="">
<a href="../UnexpectedError">UnexpectedError</a>
</li>
<li class="">
<a href="../use">use</a>
</li>
<li class="">
<a href="../withError">withError</a>
</li>
</ul>
</nav>
<div class="main" tabindex="-1">
<div class="content">
<h1 id="expect-promise-promisebody-">expect.promise(promiseBody)</h1>
<p>This method is used inside <a href="/api/addAssertion">addAssertion</a> to create
a promise from the given body function.</p>
<p>Signature:</p>
<div class="code lang-javascript">
<div>expect<span style="color: #000000">.</span><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span> <span style="color: #000000">...</span> <span style="color: #000000">});</span></div>
<div>expect<span style="color: #000000">.</span><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>run<span style="color: #000000">)</span> <span style="color: #000000">{</span> <span style="color: #000000">...</span> <span style="color: #000000">});</span></div>
<div>expect<span style="color: #000000">.</span><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span> <span style="color: #000000">...</span> <span style="color: #000000">});</span></div>
</div><p>When the promise body takes no arguments, the body will be executed.
If the body throws an exception a rejected promise will be returned.
If the body returns a promise that will be returned by the method;
otherwise a resolve promise will be returned. You can use the method
the following way:</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">var</span> promises <span style="color: #a71d5d">=</span> items<span style="color: #000000">.</span><span style="color: #000000">map</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>item<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #a71d5d">return</span> expect<span style="color: #000000">.</span><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">()</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">expect</span><span style="color: #000000">(</span>item<span style="color: #000000">,</span> <span style="color: #df5000">'to be a number'</span><span style="color: #000000">);</span></div>
<div> <span style="color: #000000">});</span></div>
<div><span style="color: #000000">});</span></div>
</div><p>When the promise body takes one argument it will be executed and given a
wrapper function that should be used to wrap asynchronous callbacks:</p>
<div class="code lang-javascript">
<div>expect<span style="color: #000000">.</span><span style="color: #000000">promise</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>run<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> backend<span style="color: #000000">.</span><span style="color: #000000">loadData</span><span style="color: #000000">(</span><span style="color: #000000">run</span><span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>err<span style="color: #000000">,</span> data<span style="color: #000000">)</span> <span style="color: #000000">{</span></div>
<div> <span style="color: #000000">expect</span><span style="color: #000000">(</span>err<span style="color: #000000">,</span> <span style="color: #df5000">'to be falsy'</span><span style="color: #000000">);</span></div>
<div> <span style="color: #000000">expect</span><span style="color: #000000">(</span>data<span style="color: #000000">,</span> <span style="color: #df5000">'not to equal'</span><span style="color: #000000">,</span> <span style="color: #000000">{});</span></div>
<div> <span style="color: #000000">}));</span></div>
<div><span style="color: #000000">});</span></div>
</div><p>If an exception is throw from within the <code>run</code>-wrapper the created
promise will be rejected with the error.</p>
<p>Notice you can call the run wrapper as many times as you want as long
as it is within the current tick, the promise will wait for all the
wrapper functions to finish before the promise will be rejected or
fulfilled.</p>
<p>When the promise body takes two arguments, it is just an alias for:</p>
<div class="code lang-javascript">
<div><span style="color: #a71d5d">new</span> Promise<span style="color: #000000">(</span><span style="color: #a71d5d">function</span> <span style="color: #000000">(</span>resolve<span style="color: #000000">,</span> reject<span style="color: #000000">)</span> <span style="color: #000000">{</span> <span style="color: #000000">...</span> <span style="color: #000000">})</span></div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
baseUrl = '../..';
</script>
<script type="text/javascript" src="../../static/toggleSidebar.js"></script>
<script type="text/javascript" src="../../static/rememberScrollPosition.js"></script>
<script type="text/javascript" src="../../static/focusMain.js"></script>
<script type="text/javascript" src="../../static/search.js"></script>
</body>
</html>