mcblekit
Version:
该微信小程序开源代码库用于管理微信小程序中的蓝牙功能。支持初始化蓝牙适配器、扫描和连接蓝牙设备、获取设备服务和特征、监听特征值变化、读写特征值以及断开连接等操作。通过设置不同的监听器,可灵活处理蓝牙连接状态变化、设备发现、服务和特征发现等事件,适用于需要与蓝牙设备进行数据交互的微信小程序开发场景。This WeChat mini program open-source code library is used to manage the Bluetooth function in WeChat mini
41 lines (38 loc) • 850 B
JavaScript
/**
* MCTimeout.js.
* MCBleKit
*
* Created by Morgan Chen on 2025/4/17.
* https://github.com/Json031
*/
/**
* Creates a timeout for function's promise.
*
* @param {function} promise function's promise.
*
* @param {timeoutMs} timeoutMs timeout milliseconds.
*
* @param {string} timeoutMessage timeout message.
*
* @return {Promise} new Promise
*
*/
function withTimeout(promise, timeoutMs, timeoutMessage = '操作超时') {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error(timeoutMessage))
}, timeoutMs)
promise
.then((res) => {
clearTimeout(timer)
resolve(res)
})
.catch((err) => {
clearTimeout(timer)
reject(err)
})
})
}
module.exports = {
withTimeout: withTimeout
};