Skip to content

Commit

Permalink
docs: correct Promise documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 12, 2021
1 parent 730ae8d commit 710951b
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions docs/src/content/docs/api/promises/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ redirect_from:
# Promises

The `t.test()`, `t.spawn()` and `t.stdin()` methods all return a
Promise which resolves to the `t` object once the child test, process,
or input stream is done. (Note that the returned Promise does *not*
resolve to the *child* object, because the child is already ended when
control returns to the parent.)
Promise which resolves to the child test results object once the child
test, process, or input stream is done.

Additionally, if the function passed to `t.test()` *returns* a
Promise, then the child test will be ended when the Promise resolves,
or failed when it is rejected.

These two features together mean that you can string together Promise
chains in your tests, if that's a thing you're into.

Unhandled promise rejections will be fail the active test, just like thrown
errors would.

Expand All @@ -34,7 +29,7 @@ t.test('get thing', t =>
t.equal(result.foo, 'bar')
t.end()
})))
.then(t =>
.then(() =>
getTwoThings()
.then(things => t.equal(things.length, 2))
.then(() => makeSomeOtherPromise())
Expand Down Expand Up @@ -67,17 +62,17 @@ The above example could be written like this:
const t = require('tap')
t.test('get thing', async t => {
const result = await getSomeThing()
await t.test('check result', async t => t.equal(result.foo, 'bar'))
}).then(async function (t) {
t.test('check result', async t => t.equal(result.foo, 'bar'))
})
t.test('two things', async t => {
const things = await getTwoThings()

const otherPromiseResult = await t.test('the things', async t => {
t.equal(things.length, 2)
}).then(() => makeSomeOtherPromise())

await t.test('check other promise thing', t => {
t.test('check other promise thing', async t => {
t.equal(otherPromiseResult, 7, 'it should be seven')
t.end()
})
})
```
Expand All @@ -91,7 +86,7 @@ For example:
```js
const t = require('tap')

async main = () => {
const main = async () => {
await t.test('get thing', t =>
getSomeThing().then(result =>
t.test('check result', async t =>
Expand All @@ -101,10 +96,10 @@ async main = () => {
const otherPromiseResult = await t.test('got two things', async t =>
t.equal(things.length, 2)).then(() => makeSomeOtherPromise())

await t.test('check other promise thing', async t =>
const childResults = await t.test('check other promise thing', async t =>
t.equal(otherPromiseResult, 7, 'it should be seven'))

console.log('tests are all done!')
console.log('tests are all done!', childResults)
}
main()
```

0 comments on commit 710951b

Please sign in to comment.