04-23-2018 05:20
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-23-2018 05:20
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
First, apologies for the lack of indenting -- pasting text into this viewer apparently loses leading spaces on each line and blank lines.
I have three procedures that use async in a .ts file (though the results are similar for the .js files since they seem to use the same transpiler)
If I don't comment on the sync on the third ("light") I get an error that Promise is not defined. Note that the cousin "lighter" does not get an error though it's identical. But If I invoke lighter then it will get the error. but not invoking doesn't make a difference. Removing "sendFile" also makes things work.
Note that I would be able to provide more help if I could compile this in Visual Studio Code but that's another conversation.
import { outbox } from "file-transfer";
declare const fetch: any;
async function sendFile() {
console.log("Sending file...");
try {
console.log("Did await");
const resp = await fetch("http://Frankston.com");
const tx1 = await resp.text()
const txt = tx1.substr(0, 20);
const data = new Uint8Array(txt.length);
for (let ctr = 0; ctr < data.length; ctr++) {
data[ctr] = txt.charCodeAt(ctr);
}
outbox.enqueue("alphabits.txt", data);
console.log("Did await");
fetch("http://Frankston.com").then((resp: any) => {
console.log(`Got response from Frankston`);
resp.text().then((txt: string) => {
console.log(`Got txt ${txt.substr(0, 10)}`);
txt = txt.split('\n')[1];
let data = new Uint8Array(txt.length);
for (let counter = 0; counter < data.length; counter++) {
data[counter] = txt.charCodeAt(counter);
}
outbox.enqueue("alphabits.txt", data);
});
});
}
catch (e) {
console.error(`Fetch error ${e}`);
}
}
function cmd(onOff:string) {
return `http://xxx/?status=${onOff}`; // URL Obfuscated
}
async function lighter(onOff:string) {
try {
const testURL = cmd(onOff);
console.log(`Sending ${testURL}`);
fetch(testURL);
console.log(`Sent ${onOff}`);
if (onOff == "off")
setInterval(() => { light("on"); }, 5 * 1000);
}
catch (e) {
console.log(`Light error ${e}`)
}
}
//async
function light(onOff: string) {
try {
const testURL = cmd(onOff);
console.log(`Sending ${testURL}`);
fetch(testURL);
console.log(`Sent ${onOff}`);
if (onOff == "off")
setInterval(() => { light("on"); }, 5 * 1000);
}
catch (e) {
console.log(`Light error ${e}`)
}
}
//lighter("off");
light("off");
setTimeout(sendFile, 2000);

04-24-2018 09:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


04-24-2018 09:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
There's an "insert code" button in the editor here.
import { outbox } from "file-transfer"; declare const fetch: any; async function sendFile() { console.log("Sending file..."); try { console.log("Did await"); const resp = await fetch("http://Frankston.com"); const tx1 = await resp.text() const txt = tx1.substr(0, 20); const data = new Uint8Array(txt.length); for (let ctr = 0; ctr < data.length; ctr++) { data[ctr] = txt.charCodeAt(ctr); } outbox.enqueue("alphabits.txt", data); console.log("Did await"); fetch("http://Frankston.com").then((resp: any) => { console.log(`Got response from Frankston`); resp.text().then((txt: string) => { console.log(`Got txt ${txt.substr(0, 10)}`); txt = txt.split('\n')[1]; let data = new Uint8Array(txt.length); for (let counter = 0; counter < data.length; counter++) { data[counter] = txt.charCodeAt(counter); } outbox.enqueue("alphabits.txt", data); }); }); } catch (e) { console.error(`Fetch error ${e}`); } } function cmd(onOff: string) { return `http://xxx/?status=${onOff}`; // URL Obfuscated } async function lighter(onOff: string) { try { const testURL = cmd(onOff); console.log(`Sending ${testURL}`); fetch(testURL); console.log(`Sent ${onOff}`); if (onOff == "off") setInterval(() => { light("on"); }, 5 * 1000); } catch (e) { console.log(`Light error ${e}`) } } //async function light(onOff: string) { try { const testURL = cmd(onOff); console.log(`Sending ${testURL}`); fetch(testURL); console.log(`Sent ${onOff}`); if (onOff == "off") setInterval(() => { light("on"); }, 5 * 1000); } catch (e) { console.log(`Light error ${e}`) } } //lighter("off"); light("off"); setTimeout(sendFile, 2000);
Typescript isn't officially supported yet, but you could try declaring promise to prevent the compiler complaining:
declare const promise: any;
Your end points need to be https for fetch().

04-24-2018 09:31 - edited 04-24-2018 09:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-24-2018 09:31 - edited 04-24-2018 09:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
OK </> -- not obvious it's insert code ..
.
I did try declaring Promise (upper case P) but that didn't do the trick.
I then converted it to .js and still get: Cannot find global value 'Promise' which is what made me assume that you use TS behind the scenes even for JS.
import { outbox } from "file-transfer"; import { encode, decode } from "cbor"; import { peerSockect } from "messaging"; import * as messaging from "messaging"; function sendData(txt, info) { try { const data = encode(txt); const fileName = `${info ? "msg" : "alphabits"}.txt` if (txt.length < (messaging.peerSocket.MAX_MESSAGE_SIZE - 32)) { // 32 for object overhead if (messaging.peerSocket.readyState == messaging.peerSocket.OPEN) { messaging.peerSocket.send({ msg: txt, info: info }); return; } else console.error(`Messaging state ${messaging.peerSocket.readyState} OPEN is ${messaging.peerSocket.OPEN}`); } outbox.enqueue(fileName, data); } catch (e) { console.error(`sendData ${e}`) } return txt; } async function sendFile() { console.log("Sending file..."); try { const resp = await fetch("https://Frankston.com"); const txt = await resp.text() sendData(txt.substr(0, 20)); //sendData(txt); } catch (e) { console.error(sendData(`Fetch error ${e}`, true)); } } function cmd(onOff) { return `http://zyx/cmd?Action=action&devID=OfficeMidFront&dn=fitbite&status=${onOff}`; } let status = "on"; // What to restore to async function light(onOff) { try { console.log(sendData(`Light ${onOff}`, true)); const testURL = cmd(onOff); const rp = fetch(testURL); rp.then((resp) => resp.json().then((js) => { console.log(`Response ${JSON.stringify(js)}`); if (js.Status) status = js.Status; } )); if (onOff == "off") setTimeout(() => { light(status); }, 3 * 1000); } catch (e) { console.log(sendData(`*Light error ${e}`, true)); } } messaging.peerSocket.onopen = () => { console.log(`PS onOpen, MAX ${messaging.peerSocket.MAX_MESSAGE_SIZE}`); messaging.peerSocket.semd({msg: "hello"}); light("off"); setTimeout(sendFile, 2000); } messaging.peerSocket.onmessage = (evt) => { console.log(`PS message ${JSON.stringify(evt.data)}`); }
(Paste code UX is strange)

04-24-2018 09:35
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-24-2018 09:35
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Responding again -- previous response was rejected as spam? OK, </> is insert code.
I did the declare (uppercase P in Promise) but no go. So I converted to .js and still got the same error message about missing Promise declaration leading me to assume ts is operating in the background anyway.
import { outbox } from "file-transfer"; import { encode, decode } from "cbor"; import { peerSockect } from "messaging"; import * as messaging from "messaging"; function sendData(txt, info) { try { const data = encode(txt); const fileName = `${info ? "msg" : "alphabits"}.txt` if (txt.length < (messaging.peerSocket.MAX_MESSAGE_SIZE - 32)) { // 32 for object overhead if (messaging.peerSocket.readyState == messaging.peerSocket.OPEN) { messaging.peerSocket.send({ msg: txt, info: info }); return; } else console.error(`Messaging state ${messaging.peerSocket.readyState} OPEN is ${messaging.peerSocket.OPEN}`); } outbox.enqueue(fileName, data); } catch (e) { console.error(`sendData ${e}`) } return txt; } async function sendFile() { console.log("Sending file..."); try { const resp = await fetch("https://Frankston.com"); const txt = await resp.text() sendData(txt.substr(0, 20)); //sendData(txt); } catch (e) { console.error(sendData(`Fetch error ${e}`, true)); } } function cmd(onOff) { return `http://zyx/?status=${onOff}`; } let status = "on"; // What to restore to async function light(onOff) { try { console.log(sendData(`Light ${onOff}`, true)); const testURL = cmd(onOff); const rp = fetch(testURL); rp.then((resp) => resp.json().then((js) => { console.log(`Response ${JSON.stringify(js)}`); if (js.Status) status = js.Status; } )); if (onOff == "off") setTimeout(() => { light(status); }, 3 * 1000); } catch (e) { console.log(sendData(`*Light error ${e}`, true)); } } messaging.peerSocket.onopen = () => { console.log(`PS onOpen, MAX ${messaging.peerSocket.MAX_MESSAGE_SIZE}`); messaging.peerSocket.semd({msg: "hello"}); light("off"); setTimeout(sendFile, 2000); } messaging.peerSocket.onmessage = (evt) => { console.log(`PS message ${JSON.stringify(evt.data)}`); } //light("off"); //setTimeout(sendFile, 2000);

04-24-2018 13:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


04-24-2018 13:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
is "semd" just a typo here?

