本文整理汇总了TypeScript中bluebird.method函数的典型用法代码示例。如果您正苦于以下问题:TypeScript method函数的具体用法?TypeScript method怎么用?TypeScript method使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了method函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: APIMethod
export function APIMethod(fn: (...args: any[]) => any): Function {
let method = Promise.method(fn);
return (request: express.Request, response: express.Response) => {
return method.call(this, request, response).then((res: Object) => {
response.status(200).json(res);
}).catch(AuthError, (err) => {
response.status(err.code).json(err.message);
}).catch(APIError, (err) => {
response.status(err.code).json(err.message);
}).catch(mongoose.Error, (err) => {
let errors = {};
// _.each(err.errors, (error, field) => {
// errors[field] = error.message;
// });
response.status(400).json(errors);
}).catch(jwt.JsonWebTokenError, (err) => {
console.error(err.stack);
response.status(401).json(err);
}).catch((err) => {
console.error(err.stack);
response.status(500).json(err.stack);
});
}
}
示例2: Function
dropPromise = Promise.mapSeries(JSON.parse(row.filters), (filter: string) => {
let fnBody = filter.trim()
if (!/^return /i.test(fnBody)) {
fnBody = 'return ' + fnBody
}
const fn = new Function('bp', 'userId', 'platform', fnBody)
return Promise.method(fn)(bp, row.userId, row.platform)
}).then(values => {
示例3: function
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
let fn: Function = descriptor.value;
descriptor.value = Promise.method((...args) => {
let req: interfaces.MyRequest = args[0];
if (!req.params[idAttribute]) {
throw new MiddlewareError('Id attribute doesn\'t set.');
}
return fn.apply(target, args);
})
};
示例4: MiddlewareMethod
export function MiddlewareMethod(fn: (...args: any[]) => any): Function {
let method = Promise.method(fn);
return (request: express.Request, response: express.Response, next: NextFunction) => {
return method.call(this, request, response, next)
.catch(function (err: Error) {
console.error(err.stack);
response.status(400).json(err.message);
});
}
}
示例5:
fooProm = Bluebird.attempt(() => {
if (fooProm) {
return fooProm;
}
return foo;
});
// - - - - - - - - - - - - - - - - -
fooProm = Bluebird.attempt(() => {
return fooThen;
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
asyncfunc = Bluebird.method(() => {});
{
const noArg: () => Bluebird<void> = Bluebird.method(() => {});
const oneArg: (x1: number) => Bluebird<void> = Bluebird.method((x1: number) => {});
const twoArg: (x1: number, x2: string) => Bluebird<void> = Bluebird.method((x1: number, x2: string) => {});
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fooProm = Bluebird.resolve(foo);
fooProm = Bluebird.resolve(fooThen);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
voidProm = Bluebird.reject(reason);
示例6: async
export default async (botId: string, bp: SDK, db: Database) => {
const emitChanged = _.throttle(() => {
bp.realtime.sendPayload(bp.RealTimePayload.forAdmins('broadcast.changed', {}))
}, 1000)
const _sendBroadcast = Promise.method((botId, row) => {
let dropPromise = Promise.resolve(false)
if (row.filters) {
dropPromise = Promise.mapSeries(JSON.parse(row.filters), (filter: string) => {
let fnBody = filter.trim()
if (!/^return /i.test(fnBody)) {
fnBody = 'return ' + fnBody
}
const fn = new Function('bp', 'userId', 'platform', fnBody)
return Promise.method(fn)(bp, row.userId, row.platform)
}).then(values => {
return _.some(values, v => {
if (!_.isBoolean(v)) {
bp.logger.warn('Filter returned something other ' + 'than a boolean (or a Promise of a boolean)')
}
return typeof v !== 'undefined' && v !== null && v !== true
})
})
}
return dropPromise.then(async drop => {
if (drop) {
bp.logger.debug(`Drop sending #${row.scheduleId} to user: ${row.userId}. Reason = Filters`)
return
}
const content = await bp.cms.getContentElement(botId, row.text)
return bp.events.sendEvent(
bp.IO.Event({
botId,
channel: row.platform,
target: row.userId,
type: 'text',
direction: 'outgoing',
payload: content.formData
})
)
})
})
const trySendBroadcast = async (broadcast, { scheduleUser, scheduleId }) => {
await retry(() => _sendBroadcast(botId, broadcast), {
max_tries: 3,
interval: 1000,
backoff: 3
})
await db.deleteBroadcastOutbox(scheduleUser, scheduleId)
await db.increaseBroadcastSentCount(scheduleId)
}
const handleFailedSending = async (err, scheduleId) => {
bp.logger.error(`Broadcast #${scheduleId}' failed. Broadcast aborted. Reason: ${err.message}`)
bp.notifications.create(botId, {
botId,
level: 'error',
message: 'Broadcast #' + scheduleId + ' failed.' + ' Please check logs for the reason why.'
})
await db.updateErrorField(scheduleId)
await db.deleteBroadcastOutboxById(scheduleId)
}
async function scheduleToOutbox(botId) {
const { schedulingLock } = await bp.kvs.get(botId, 'broadcast/lock/scheduling')
if (!db.knex || schedulingLock) {
return
}
const inFiveMinutes = moment()
.add(5, 'minutes')
.toDate()
const endOfDay = moment(inFiveMinutes)
.add(14, 'hours')
.toDate()
const upcomingFixedTime = db.knex.date.isAfter(inFiveMinutes, 'ts')
const upcomingVariableTime = db.knex.date.isAfter(endOfDay, 'date_time')
await bp.kvs.set(botId, 'broadcast/lock/scheduling', { schedulingLock: true })
const schedules = await db.getBroadcastSchedulesByTime(botId, upcomingFixedTime, upcomingVariableTime)
await Promise.map(schedules, async schedule => {
const timezones = await db.getUsersTimezone()
await Promise.mapSeries(timezones, async tz => {
//.........这里部分代码省略.........
示例7: Function
const _sendBroadcast = Promise.method(row => {
let dropPromise = Promise.resolve(false)
if (row.filters) {
dropPromise = Promise.mapSeries(JSON.parse(row.filters), filter => {
let fnBody = filter.trim()
if (!/^return /i.test(fnBody)) {
fnBody = 'return ' + fnBody
}
const fn = new Function('bp', 'userId', 'platform', fnBody)
return Promise.method(fn)(bp, row.userId, row.platform)
}).then(values => {
return _.some(values, v => {
if (v !== true && v !== false) {
bp.logger.warn('Filter returned something other ' + 'than a boolean (or a Promise of a boolean)')
}
return typeof v !== 'undefined' && v !== null && v !== true
})
})
}
return dropPromise.then(drop => {
if (drop) {
bp.logger.debug(`Drop sending #${row.scheduleId} to user: ${row.userId}. Reason = Filters`)
return
}
return bp.renderers.sendToUser(row.userId, '#!' + row.text)
})
})