Skip to content

Promise then/catch 未执行,只执行了finally #166

@laxian

Description

@laxian

问题描述:
鸿蒙手机,蓝牙发送数据,Promise 没有执行then/catch方法,但执行了finally。
使用了ArkRuntime,创建了event loop。

复现代码:

// Arkts 方法原型:        
// writeCharacteristicValue(characteristic: BLECharacteristic, writeType: GattWriteType): Promise<void>;
pub async fn write_characteristic(
        env: &Env,
        uuid: &Uuid,
        service_uuid: &Uuid,
        data: &[u8],
        write_type: WriteType
    ) -> Result<()> {

        let ble = env.load("@ohos.bluetooth.ble").map_err(|e|{
            Error::Other(Box::new(e))
        })?;

        // 获取 gatt对象
        let client = ble
            .call::<&str, &str, JsObject>("createGattClientDevice", addr)
            .expect("Failed to call createGattClientDevice");

        // 获取 writeCharacteristicValue 方法
        let write_method: Function<FnArgs<(JsObject, u32)>, PromiseRaw<()>> = client
            .get("writeCharacteristicValue")?
            .unwrap();

        // 获取特性对象
        let mut characteristic = get_characteristic_by_uuid(uuid, service_uuid)?;

        // 设置properties
        let mut properties = env.create_object()?;
        properties.set("write", true)?; // Example: set write property to false
        properties.set("writeNoResponse", true)?; // Example: set notify property to true
        characteristic.set("properties", properties)?;

        // 设置特性值
        let buffer = Buffer::from(data.to_vec());
        characteristic.set_named_property("characteristicValue", buffer)?;

        // 确定写入类型
        let write_type_value = match write_type {
            WriteType::WithResponse => 1u32,
            WriteType::WithoutResponse => 2u32,
        };

        // 获取 Promise 对象并调用 then 方法
        let promise = write_method.apply(client, FnArgs {
            data: (characteristic, write_type_value),
        })?;

        promise
            .then(move |a| {
                Ok(())
            })
            .expect("then failed")
            .catch(|e: CallbackContext<()>| {
                Ok(())
            })
            .expect("catch failed")
            .finally(|f| {
                Ok(())
            })
            .expect("finally failed");

        // TODO: wait then 
        return Ok(())
}

    fn get_characteristic_by_uuid(env: &Env, uuid: &Uuid, service_uuid: &Uuid) -> Result<Object> {
        let mut characteristic = env.create_object()?;
        characteristic.set("characteristicUuid", uuid.to_string())?;
        characteristic.set("serviceUuid", service_uuid.to_string())?;

        // Use a Buffer instead of an ArrayBuffer
        let buffer = Buffer::from(vec![0u8]); // Create a 1-byte Buffer
        characteristic.set("characteristicValue", buffer)?;

        // Set properties as an object matching GattProperties
        let properties = env.create_object()?;
        characteristic.set("properties", properties)?;

        // Set descriptors as an empty array
        let descriptors = env.create_array_with_length(0)?; // Create an empty array
        characteristic.set("descriptors", descriptors)?;

        Ok(characteristic)
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions