8.typescript代码练习
import { error } from "jquery"
function open_door()
{console.log('door is opened')}
function GetValue():string{
return 'ok'
}
open_door()
let value2 = GetValue()
console.log('value is ',value2)
setTimeout(() => {
console.info('A1')
}, 1);
setTimeout(() => {
console.info('B1')
}, 2)
console.info('C1')
// import * as fs11 from 'fs'
// fs11.readFile( //NodeJs异步API
// '/var/log/apache2/access_log',
// {encoding:'utf8'},
// (error,data)=>{
// if(error){
// console.error('error:',error)
// return
// }
// console.info('data is:',data)
// }
// )
function appendAndReadPromise(path:string,data:string):Promise<string>{
return appendPromise(path,data)
.then(()=>ReadPromise(path))
.catch(error=>console.error(error))
}
function appendAndRead(path:string,
data:string,
cb:(error:Error|null,result:string|null)=>void
){
appendFile(path,data,error=>{
if(error){
return cb(error,null)
}
readFile(path,(error,result)=>{
if(error){
return cb(error,null)
}
cb(null,result)
})
})
}
//MainThread.ts//多线程
let worker1 = new Worker('workerScript.js')
//worker1.postMessage('some data') //通过postMessage API几乎可以把任何数据传给另一个线程
//如果想反过来通讯,由职程发消息给主线程,使用全局可用的postMessage把消息发给主线程,在主线程中使用.onmessage方法监听入站消息
worker1.onmessage=e=>{
console.log(e.data)//接收数据
}
worker1.postMessage('some data')
//WorkerScript.ts
onmessage = e=>{
console.log(e.data)
postMessage('Ack:"${e.data}"')
}
欢迎讨论,相互学习。
cdtxw@foxmail.com