-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
promise-retry.js
49 lines (43 loc) 路 1.09 KB
/
promise-retry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
const Hemera = require('./../../packages/hemera')
const nats = require('nats').connect()
const promiseRetry = require('promise-retry')
const hemera = new Hemera(nats, {
logLevel: 'silent'
})
hemera.ready(() => {
const opt = {
retries: 2, // The maximum amount of times to retry the operation
factor: 2, // The exponential factor to use
minTimeout: 1000, // The number of milliseconds before starting the first retry
maxTimeout: 10000, // The maximum number of milliseconds between two retries
randomize: false // Randomizes the timeouts by multiplying with a factor between 1 to 2
}
hemera.add(
{
topic: 'math',
cmd: 'add'
},
function*(req, cb) {
return Promise.reject(new Error('Uuups!'))
}
)
promiseRetry(function(retry, number) {
console.log('Attempt number', number)
return hemera
.act({
topic: 'math',
cmd: 'add',
a: 1,
b: 20
})
.catch(retry)
}, opt).then(
function(value) {
console.log(value)
},
function(err) {
console.error(err)
}
)
})