本文整理汇总了TypeScript中thunks.thunk.delay方法的典型用法代码示例。如果您正苦于以下问题:TypeScript thunk.delay方法的具体用法?TypeScript thunk.delay怎么用?TypeScript thunk.delay使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类thunks.thunk
的用法示例。
在下文中一共展示了thunk.delay方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
//.........这里部分代码省略.........
if (rand < 0.2) {
setTimeout(() => serverListener.call(this, msg, rinfo), rand * 10) // out-of-order
} else {
serverListener.call(this, msg, rinfo)
}
})
const cli1 = new Client()
cli1.setKeepAlive(true)
cli1.on('error', (err) => { ilog(Object.assign(err, { class: 'client error' })) })
await cli1.connect(server.address().port)
const clientListener = cli1[kSocket].listeners('message')[0]
cli1[kSocket].removeListener('message', clientListener)
cli1[kSocket].addListener('message', function (msg: Buffer, rinfo: AddressInfo) {
const rand = Math.random()
if (rand < lossRatio) {
return // packet loss
}
if (rand < 0.2) {
setTimeout(() => clientListener.call(this, msg, rinfo), rand * 10) // out-of-order
} else {
clientListener.call(this, msg, rinfo)
}
})
const stream1 = cli1.request()
stream1.on('error', (err) => { throw Object.assign(err, { class: 'client stream error' })})
const dataStream1 = new RandDataStream(bytes)
stream1.on('drain', () => {
dataStream1.resume()
})
// randDataStream.pipe(stream) // use paused mode for large stream
dataStream1
.on('data', (chunk) => {
if (!stream1.write(chunk)) {
dataStream1.pause()
}
})
.on('end', () => stream1.end())
const hash1 = createHash('sha256')
stream1.pipe(hash1)
const cli2 = await cli1.spawn(server.address().port)
cli2.setKeepAlive(true)
const stream2 = cli2.request()
const stream3 = cli2.request()
stream2.on('error', (err) => { throw Object.assign(err, { class: 'client stream error' })})
stream3.on('error', (err) => { throw Object.assign(err, { class: 'client stream error' })})
const dataStream2 = new RandDataStream(bytes)
stream3.on('drain', () => {
dataStream2.resume()
})
dataStream2
.on('data', (chunk) => {
stream2.write(chunk)
if (!stream3.write(chunk)) {
dataStream2.pause()
}
})
.on('end', () => {
stream2.end()
stream3.end()
})
const hash2 = createHash('sha256')
stream2.pipe(hash2)
const hash3 = createHash('sha256')
stream3.pipe(hash3)
await Promise.all([
onceEmit.call(stream1, 'end'),
onceEmit.call(stream2, 'end'),
onceEmit.call(stream3, 'end'),
])
equal(dataStream1.readBytes, bytes)
equal(dataStream1.totalSize, bytes)
equal(stream1.bytesRead, bytes)
equal(stream1.bytesWritten, bytes)
const ret1 = hash1.read() as Buffer
equal(ret1.toString('hex'), dataStream1.sum)
equal(dataStream2.readBytes, bytes)
equal(dataStream2.totalSize, bytes)
equal(stream2.bytesRead, bytes)
equal(stream2.bytesWritten, bytes)
const ret2 = hash2.read() as Buffer
equal(ret2.toString('hex'), dataStream2.sum)
equal(stream3.bytesRead, bytes)
equal(stream3.bytesWritten, bytes)
const ret3 = hash3.read() as Buffer
equal(ret3.toString('hex'), dataStream2.sum)
await Promise.all([cli1.close(), cli2.close()])
await server.close()
await thunk.promise(thunk.delay(500))
})
示例2:
tman.it('test level 2-2', function * () {
yield thunk.delay(10)
assert.strictEqual(count++, 4)
})
示例3: function
tman.it('test level 1-1', function () {
this.timeout(1000)
assert.strictEqual(count++, 21)
return thunk.delay(100)
})
示例4: return
return (function *() { yield thunk.delay(20) })()