Skip to content

Commit

Permalink
missing awaits
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 26, 2023
1 parent 16afaa5 commit 64668d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
31 changes: 21 additions & 10 deletions packages/sampleprj/src/mainthingspeak.ts
@@ -1,4 +1,4 @@
import { fetch } from "@devicescript/net"
import { fetch, Response } from "@devicescript/net"
import { readSetting } from "@devicescript/settings"

/**
Expand All @@ -23,21 +23,32 @@ export async function writeData(
}
) {
const url = "https://api.thingspeak.com/update.json"
// the secret key should be in .env.local
const key = await readSetting("TS_KEY")

// construct payload
const payload: any = {}
// field values
Object.keys(fields).forEach(k => {
const v = fields[k]
if (v !== undefined && v !== null) payload[`field${k}`] = v
})
// additional options
if (options) Object.assign(payload, options)

return await fetch(url, {
method: "POST",
headers: {
THINGSPEAKAPIKEY: key,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
}
// send request and return http response
let resp: Response
try {
resp = await fetch(url, {
method: "POST",
headers: {
THINGSPEAKAPIKEY: key,
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
return resp.status
} finally {
await resp?.close()
}
}
2 changes: 1 addition & 1 deletion website/docs/developer/iot/matlab-thingspeak/index.mdx
Expand Up @@ -66,7 +66,7 @@ export async function writeData(
})
return resp.status
} finally {
resp?.close()
await resp?.close()
}
}
```

0 comments on commit 64668d0

Please sign in to comment.